は、私が新しく作成したアカウントにログインしようとしたとき、私は問題に実行していたユーザーアカウントの作成が作業を得ることができた:?Rails - Authlogic:未定義のメソッド 'valid_password?'
未定義のメソッド `valid_password」
とエラーが
if @user_session.save!
私はRailsの5にAuthlogicとbcryptのだと私はなぜか、どのように上記のエラーを修正するかわからない
を強調しています。 Authlogicをセットアップするために this guideを使用しましたが、サインアウトすることができましたが、ログインしようとすると上記のエラーが発生します。いくつかの検索では、 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 04-11-17 行った後以下の変更は、私のために働いた私は上記の問題がなければログアウトすることができました。私はまだそれが私にエラーを与えた理由を知らないが、パスワードが有効かどうかをチェックする機能を持たない "has_secure_password"( "valid_password?")を使用していたためだと思われる。