我设法让用户帐户创建工作但是当我尝试登录新创建的帐户时遇到问题:
未定义的方法`valid_password?'
,错误突出显示
if @user_session.save!
我是Rails 5上的Authlogic和BCrypt,我不知道为什么或如何解决上面的错误。我使用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
编辑04-11-17 在进行下面的更改后,它对我有用,我能够在没有上述问题的情况下注销。我仍然不知道为什么它给了我错误,但我怀疑是因为我使用的是“has_secure_password”,它没有检查密码是否有效的功能(“valid_password?”)。
答案 0 :(得分: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_password”和“password_salt”。完成上述编辑后,我能够成功登录和退出,而不会出现上述错误。