0

当我登录第 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:increate'

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

1 回答 1

0

查看http://ruby.railstutorial.org/chapters/sign-in-sign-out上的清单 8.20 。看起来您错过了定义current_user=方法的步骤

  def current_user=(user)
    @current_user = user
  end
于 2013-09-10T17:26:26.607 回答