2017-04-11 59 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」。完成上述編輯後,我能夠成功登錄和註銷,而不會出現上述錯誤。

相關問題