SessionsController中的NoMethodError #create(未定义的方法`current_user)Hartl第9章

时间:2013-09-10 17:17:11

标签: ruby-on-rails ruby ruby-on-rails-4

当我在第9章结尾处签署我从Hartl的轨道教程制作的示例应用程序时,我收到此错误。我搜索过高低,但无法弄清楚可能非常明显的东西。

  

SessionsController中的NoMethodError #create

     

#session ....

的未定义方法`current_user ='

这两个文件中出现错误,代码如下

  

app / helpers / sessions_helper.rb:7:在sign_in' app/controllers/sessions_controller.rb:9:in创建'

应用/助手/ 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

1 个答案:

答案 0 :(得分:0)

查看http://ruby.railstutorial.org/chapters/sign-in-sign-out上的8.20列表。您似乎错过了定义current_user=方法

的步骤
  def current_user=(user)
    @current_user = user
  end