所以,我有这个错误:
undefined local variable or method session for ApplicationController:Class
所以我猜测的是rails,在设置之前没有"session"变量,对吧?或者我做错了什么.这是使用它的代码:
class ApplicationController < ActionController::Base # Pre vent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception @account = User.find session[:userid] end
我在登录页面设置了它.是否只有一种方法可以执行它是不是零,还是不会因为它真的未定义而无效?我检查会议对吗?设置会话的代码:
class SessionsController < ApplicationController def login end def login_attempt authuser = User.auth(params[:name], params[:password]) if authuser session[:userid] = authuser.id flash[:notice] = "We logged you in, #{authuser.name}!" else flash[:notice] = "INVALID USERNAME/PASSWORD!" end end
结束
所以...任何想法这个错误信息甚至意味着什么.我看到有这样的问题,但根据我的测试,这是不一样的(或者是吗?)
session
方法确实存在,但它仅在实例上,而不是在类级别上.
在您的代码中,您@account
在类级别定义.那是错的.
改为在实例方法中:
def foo @account = User.find session[:something] end