未定义的方法`authenticate&#39;对于#<user:xxxxxxxxxxx> </user:xxxxxxxxxxx>

时间:2015-03-29 08:28:37

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

我是ROR的新手,并尝试使用用户身份验证制作一个简单的网站。确认电子邮件有问题,它工作正常,但当我想登录时,我收到此错误:

NoMethodError(未定义的方法authenticate' for #<User:0x007fd2863d6b88>): app/controllers/sessions_controller.rb:12:in创建&#39;

要做这个代码,我使用了2个教程,一个是Mickael Hartl https://www.railstutorial.org/book/updating_and_deleting_users#cha-updating_showing_and_deleting_users和一个Rory Koehler用于确认电子邮件:https://coderwall.com/p/u56rra/ruby-on-rails-user-signup-email-confirmation-tutorial

我认为这一行存在问题: user = User.find_by_email(params [:session] [:email])

这里是我的sessions_controller.rb的代码:

def create
user = User.find_by_email(params[:session][:email])
if user && user.authenticate(params[:session][:password])
  if user.email_confirmed
    sign_in user
    redirect_back_or user
  else
    flash.now[:error] ='Vous devez activer votre compte avec le mail de     confirmation que nous vous avons envoyé pour pouvoir continuer.'
    render "new"
  end
else
  flash.now[:error] = 'Combinaison Utilisateur/mot de passe invalide'
  render 'new'
end
end

用户模型:user.rb

class User < ActiveRecord::Base

attr_accessor :password
attr_accessible :name, :email, :login, :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 :login, :presence => true,
                  :length   => { :maximum => 20 }

validates :password, :presence     => true,
                   :confirmation => true,
                   :length       => { :within => 6..40 }

before_save :encrypt_password
before_create :confirmation_token

      # Retour true (vrai) si le mot de passe correspond.
  def has_password?(password_soumis)
    encrypted_password == encrypt(password_soumis)
    # Compare encrypted_password avec la version cryptée de
    # password_soumis.
  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

    def email_activate
        self.email_confirmed = true
        self.confirm_token = nil
        save!(:validate => false)
    end


  private

    def confirmation_token
      if self.confirm_token.blank?
          self.confirm_token = SecureRandom.urlsafe_base64.to_s
      end
    end

    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

任何想法:)?

1 个答案:

答案 0 :(得分:0)

您已将authenticate定义为类方法:

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

但是你把它称为实例方法:

user.authenticate(params[:session][:password])

要修复它,您应该将authenticate方法更改为:

def authenticate(submitted_password)
  self.has_password?(submitted_password)
end