我想知道为什么我的rails没有保存我的encrypted_password字段。
这是我的UserController
class UserController < ApplicationController
require 'bcrypt'
before_filter :save_login_state, :only => [:new, :create]
def new
new_user = User.new(user_params)
new_user.numRatings = 0
if new_user.save
flash[:notice] = "You signed up successfully"
flash[:color]= "valid"
else
flash[:notice] = "Form is invalid"
flash[:color]= "invalid"
end
redirect_to(:controller => 'sessions', :action => 'login')
end
def create
end
def update
end
def view
end
private
def user_params
params.require(:user).permit(:name, :email, :password, :password_confirmation)
end
end
这是我的用户模型
class User < ActiveRecord::Base
require 'BCrypt'
attr_accessor :password, :encrypted_password
EMAIL_REGEX = /\A[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\z/i
validates :email, :presence => true, :uniqueness => true, :format => EMAIL_REGEX
validates :name, :presence => true
validates :password, :confirmation => true, :presence => true, :on => :create
before_save :encrypt_password
after_save :clear_password
def encrypt_password
if password.present?
self.salt = BCrypt::Engine.generate_salt
self.encrypted_password = BCrypt::Engine.hash_secret(password, salt)
end
end
def clear_password
self.password = nil
end
def self.authenticate(username_or_email="", login_password="")
if EMAIL_REGEX.match(username_or_email)
user = User.find_by_email(username_or_email)
end
if user && user.match_password(login_password)
return user
else
return false
end
end
def match_password(login_password="")
encrypted_password == BCrypt::Engine.hash_secret(login_password, salt)
end
end
另外,我使用此功能来保存它
def login_attempt
authorized_user = User.authenticate(params["user"]["email"],params["user"]["password"])
if authorized_user
session[:user_id] = authorized_user.id
flash[:notice] = "Wow Welcome again, you logged in as #{authorized_user.username}"
redirect_to(:action => 'home')
else
flash[:notice] = "Invalid Username or Password"
flash[:color]= "invalid"
render "login"
end
end
我怀疑我创建了第一次迁移
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :email
t.string :password
t.string :name
t.float :rating
t.integer :numRatings
t.timestamps
end
end
end
但是,我将:password字段更改为:encrypted_password,并将其反映在表格中。我已经坚持了2个小时。我想知道是否有任何事情发生。感谢。
日志表示正在注入数据减去加密密码
INSERT INTO&#34;用户&#34; (&#34; created_at&#34;,&#34; email&#34;,&#34; name&#34;,&#34; numRatings&#34;,&#34; salt&#34;,&#34; updated_at&#34;)VALUES(?,?,?,?,?,?)[[&#34; created_at&#34;,&#34; 2014-08-05 06:22:41.335373&#34;], [&#34;电子邮件&#34;,&#34; w@w.com"],[&#34;名称&#34;,&#34; w&#34;],[&#34; numRatings& #34;,0],[&#34;盐&#34;,&#34; $ 2a $ 10 $ pagmnNzT4FWEBmaPmiLX1u&#34;],[&#34; updated_at&#34;,&#34; 2014-08-05 06:22:41.335373&#34;]]
答案 0 :(得分:4)
你的attr_accessor :encrypted_password
突然出现 - 这会覆盖Rails生成的属性getter / setter,只会在模型实例中设置一个名为@encrypted_password
的实例变量。
答案 1 :(得分:0)
您提到您已将迁移:password field
更改为:encrypted_password field
,但是您确定要重新初始化模型吗?即正确迁移了这些变化?最后,如果您确实更改了迁移,则不需要attr_accessor
encrypted_password
。拥有它们可能会导致阴影并成为问题。