2017-04-11 27 views
1

我设法不过用户帐户的创建工作我遇到了一个问题,当我尝试登录到新创建的帐户:Rails - Authlogic:未定义的方法'valid_password?'

未定义的方法`valid_password”

和错误突出

if @user_session.save! 

我Authlogic和BCrypt on Rails的5,我不知道为什么或如何解决上述错误。我使用this guide为了设置Authlogic和我能够注销,但是当试图登录时,我得到了上述错误。从一些搜索,有些人已经能够通过restarting Heroku解决这个问题,但我没有使用Heroku,所以我不相信这对我有用。另一种解决方案是add some password fields,但我相信“has_secure_password”会相应地提供这些字段。有谁知道为什么发生此错误和/或如何解决它?谢谢!

用户型号:

class User < ApplicationRecord 

    has_many :activities 
    has_secure_password 

    validates :first_name, presence: true, length: {minimum: 1} 
    validates :last_name, presence: true, length: {minimum: 1} 
    validates :email, presence: true, uniqueness: true, length: {minimum: 5} 
    validates :password_digest, length: {minimum: 6} 
    validates :password, :confirmation => true, length: {minimum: 4} 
    validates :password_confirmation, presence: true 

    #-----------------------New Stuff --------------------------------------- 
    acts_as_authentic do |c| 
     c.crypto_provider = Authlogic::CryptoProviders::BCrypt 
    end 

    #------------------------------------------------------------------------ 

    #---------------Unsure if working-------------- 
    #validates_presence_of :password, :on => :create 
    #validates_presence_of :email 
    #validates_uniqueness_of :email 
    #---------------------------------------------- 

    def self.authenticate(email, password) 
     user = find_by_email(email) 
     if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt) 
     user 
     else 
     nil 
     end 
    end 

    def encrypt_password 
     if password.present? 
     self.password_salt = BCrypt::Engine.generate_salt 
     self.password_hash = BCrypt::Engine.hash_secret(password, password_salt) 
     end 
    end 
    end 

用户会话控制器:

class UserSessionsController < ApplicationController 

    def new 
    @user_session = UserSession.new 
    end 

    def create 
    @user_session = UserSession.new(user_session_params) 
    if @user_session.save! 
     flash[:success] = 'Welcome back' 
     redirect_to root_path 
    else 
     render :new 
    end 
    end 

    def destroy 
    current_user_session.destroy 
    flash[:success] = 'Goodbye' 
    #redirect_to root_path - Should redirect somewhere after logout 
    end 

    private 

    def user_session_params 
    params.require(:user_session).permit(:email,:password) 
    end 

end 

当前用户和用户会话表:

create_table "user_sessions", force: :cascade do |t| 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

    create_table "users", force: :cascade do |t| 
    t.string "first_name" 
    t.string "last_name" 
    t.string "email" 
    t.datetime "created_at",  null: false 
    t.datetime "updated_at",  null: false 
    t.string "persistence_token" 
    t.string "password_digest" 
    t.index ["email"], name: "index_users_on_email", unique: true, using: :btree 
    end 

EDIT 17年4月11日 使得后下面的变化,它为我工作我可以在没有提到的问题的情况下注销。我仍然不知道为什么它给了我错误,但我怀疑这是因为我使用“has_secure_password”,它没有函数来检查密码是否有效(“valid_password?”)。

回答

0

好吧,事实证明,我设法解决它通过删除“has_secure_password”,只依靠“acts_as_authentic”。之后,我能够成功登录。这里是我更新的代码:

用户模型:

class User < ApplicationRecord 

    has_many :activities 
    acts_as_authentic 

    validates :first_name, presence: true, length: {minimum: 1} 
    validates :last_name, presence: true, length: {minimum: 1} 
    validates :email, presence: true, uniqueness: true, length: {minimum: 5} 
    validates :password, :confirmation => true, length: {minimum: 4} 
    validates :password_confirmation, presence: true 

    #-----------------------New Stuff --------------------------------------- 


    #------------------------------------------------------------------------ 

    #---------------Unsure if working-------------- 
    #validates_presence_of :password, :on => :create 
    #validates_presence_of :email 
    #validates_uniqueness_of :email 
    #---------------------------------------------- 

    def self.authenticate(email, password) 
     user = find_by_email(email) 
     if user && user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt) 
     user 
     else 
     nil 
     end 
    end 

    def encrypt_password 
     if password.present? 
     self.password_salt = BCrypt::Engine.generate_salt 
     self.password_hash = BCrypt::Engine.hash_secret(password, password_salt) 
     end 
    end 
    end 

用户表:

create_table "users", force: :cascade do |t| 
    t.string "first_name" 
    t.string "last_name" 
    t.string "email" 
    t.datetime "created_at",  null: false 
    t.datetime "updated_at",  null: false 
    t.string "persistence_token" 
    t.string "password_digest" 
    t.string "crypted_password", null: false 
    t.string "password_salt" 
    t.index ["email"], name: "index_users_on_email", unique: true, using: :btree 
    end 

并非所有上述要求的领域,但我有他们,因为我还是新的Rails。主要的事情是“crypted_pa​​ssword”和“password_salt”。完成上述编辑后,我能够成功登录和注销,而不会出现上述错误。

相关问题