Michael Hartl的Rails教程:未定义的方法`authenticate_with_salt'错误

时间:2014-02-19 04:58:14

标签: ruby-on-rails ruby

我已经到了第9章的末尾,我在根页面上出现了错误。

会议助手是

module SessionsHelper
  def sign_in(user)
    cookies.permanent.signed[:remember_token] = [user.id, user.salt]
    current_user = user
  end
  def current_user
    @current_user ||= user_from_remember_token
  end
  def signed_in?
    !current_user.nil?
  end
  def sign_out
    cookies.delete(:remember_token)
    current_user = nil
  end
  private

    def user_from_remember_token
      User.authenticate_with_salt(*remember_token)
    end

    def remember_token
      cookies.signed[:remember_token] || [nil, nil]
    end
end

这是我的user.rb

require 'digest'
class User < ActiveRecord::Base
  attr_accessor :password
  attr_accessible :name, :email, :password, :password_confirmation
  email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i

  validates :name, :presence => true,
                    :length => { :maximum => 50 }
  validates :email, :presence => true,
                    :format => { :with => email_regex },
                    :uniqueness => { :case_sensitive => false }
  validates :password, :presence => true,
                        :confirmation => true,
                        :length => {:within => 6..40 }
  before_save :encrypt_password
  def signed_in?
    !current_user.nil?


  def has_password?(submitted_password)
    encrypted_password == encrypt(submitted_password)  
  end

  def self.authenticate(email, submitted_password)
    user = find_by_email(email)
    return nil if user.nil?
    return user if user.has_password?(submitted_password)
  end

  def self.authenticate_with_salt(id, cookie_salt)
    user = find_by_id(id)
    (user && user.salt == cookie_salt)? user : nil
  end

  private
    def encrypt_password
      self.salt = make_salt if new_record?
      self.encrypted_password = encrypt(password)
    end
    def encrypt(string)
      secure_hash("#{salt}--#{string}")
    end
    def make_salt
      secure_hash("#{Time.now.utc}--#{password}")
    end
    def secure_hash(string)
      Digest::SHA2.hexdigest(string)
    end                                         
  end
end

我的session_controller是

class SessionsController < ApplicationController
  def new
    @title = "Sign in"
  end

  def create
    user = User.authenticate(params[:session][:email], params[:session][:password])
    if user.nil?
      flash.now[:error] = "Invalid email/password combination."
      @title = "Sign in"
      render 'new'
    else
      sign_in user
      redirect_to user  
    end
  end

  def destroy
    sign_out
    redirect_to root_path
  end
end

和users_controler是

class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
    @title = @user.name
  end

  def new
    @user = User.new
    @title = "Sign up"
  end

  def create
    @user = User.new(params[:user])
    if @user.save
      sign_in @user
      flash[:success] = "Welcome to the Sample App!"
      redirect_to @user
    else
      @title = "Sign up"
      render 'new'
    end
  end
end

事情是我在Rails中是一个初学者,我正在阅读这个教程,因为它被困在这里。 请帮助guyz

这是错误的屏幕截图 Screenshot_error

错误说:未定义的方法'authentication_with_salt'

1 个答案:

答案 0 :(得分:2)

我认为这是类似的问题,链接是here

您错过了current_user范围。请查看以上链接,希望您尽快解决问题。