当我登录第 9 章末尾的 Hartl's rail tutorial 制作的示例应用程序时,我得到了这个错误。我搜索了高低,但无法弄清楚可能非常明显的内容。
SessionsController#create 中的 NoMethodError
#session 的未定义方法 `current_user='....
错误发生在这两个文件中,代码如下
app/helpers/sessions_helper.rb:7:in
sign_in' app/controllers/sessions_controller.rb:9:in
create'
应用程序/助手/sessions_helper
module SessionsHelper
def sign_in(user)
remember_token = User.new_remember_token
cookies.permanent[:remember_token] = remember_token
user.update_attribute(:remember_token, User.encrypt(remember_token))
self.current_user = user
end
def signed_in?
!current_user.nil?
end
def current_user
remember_token = User.encrypt(cookies[:remember_token])
@current_user ||= User.find_by(remember_token: remember_token)
end
def current_user?(user)
user == current_user
end
def sign_out
self.current_user = nil
cookies.delete(:remember_token)
end
def redirect_back_or(default)
redirect_to(session[:return_to] || default)
session.delete(:return_to)
end
def store_location
session[:return_to] = request.url if request.get?
end
end
应用程序/控制器/sessions_controller
class SessionsController < ApplicationController
def new
end
def create
user = User.find_by(email: params[:session][:email].downcase)
if user && user.authenticate(params[:session][:password])
sign_in user
redirect_back_or user
else
flash.now[:error] = 'Invalid email/password combination'
render 'new'
end
end
def destroy
sign_out
redirect_to root_url
end
end