Rails 不保存模型字段

Gar*_*gob 1 ruby ruby-on-rails

我想知道为什么我的 Rails 没有保存我的 crypto_password 字段。

这是我的用户控制器

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
Run Code Online (Sandbox Code Playgroud)

这是我的用户模型

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
Run Code Online (Sandbox Code Playgroud)

另外我用这个函数来保存

  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
Run Code Online (Sandbox Code Playgroud)

我怀疑我创建了我的第一次迁移

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
Run Code Online (Sandbox Code Playgroud)

但是,我将 :password 字段更改为 :encrypted_pa​​ssword ,它反映在表中。我已经被困在这个问题上2个小时了。我想知道是否有什么突出的地方。谢谢。

日志显示正在注入的数据减去加密密码

INSERT INTO "users" ("created_at", "email", "name", "numRatings", "salt", "updated_at") VALUES (?, ?, ?, ?, ?, ?) [["created_at", "2014-08-05 06:22:41.335373"], ["电子邮件", "w@w.com"], ["姓名", "w"], ["numRatings", 0], ["盐" , "$2a$10$pagmnNzT4FWEBmaPmiLX1u"], ["updated_at", "2014-08-05 06:22:41.335373"]]

sev*_*cat 5

你的attr_accessor :encrypted_password突出之处 - 这是用一个简单地设置模型实例中调用的实例变量的方法覆盖 Rails 生成的属性 getter/setter @encrypted_password

  • “attr_accessible”不是“attr_accessor”。`attr_accessible` 是 Rails 中的一项安全功能 - http://guides.rubyonrails.org/v3.2.18/security.html#mass-assignment。`attr_accessor` 是 Ruby 的一项功能 - http://www.ruby-doc.org/core-2.1.2/Module.html#method-i-attr_accessor (2认同)