我一直在用Ruby on Rails开发一些Web应用程序,然后当我转到:localhost:3000I get this error:
undefined method `session_token' for nil:NilClass
if (user_id = session[:user_id])
user = User.find_by(id: user_id)
@current_user ||= user if session[:session_token] == user.session_token
字符串
红色标记在这条线上:
@current_user ||= user if session[:session_token] == user.session_token
型
completel current_user方法如下:
def current_user
if (user_id = session[:user_id])
user = User.find_by(id: user_id)
@current_user ||= user if session[:session_token] == user.session_token
elsif (user_id = cookies.encrypted[:user_id])
user = User.find_by(id: user_id)
if user&.authenticated?(:remember, cookies[:remember_token])
log_in user
@current_user = user
end
end
end
型
1条答案
按热度按时间5jvtdoz21#
将
find_by
更改为find_by!
:字符串