Rails更新控制器中的列

时间:2013-08-30 01:00:38

标签: ruby-on-rails crud

我有这个用户模型

class User < ActiveRecord::Base

  attr_accessible :email, :name, :password_hash, :password_salt, :secret, :surname, :code, :updated_by
  attr_accessible :password, :password_confirmation
  attr_accessor :password
  attr_protected :secret

  before_save :encrypt_password

  validates_presence_of :name
  validates_presence_of :email
  validates_uniqueness_of :email
  validates_confirmation_of :password
  validates_presence_of :password, :on => :create

  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

  def self.generate_code
      Array.new(32){Random.new.rand(36).to_s(36)}.join
  end

... etc
end

并且在前端控制器中我这样做

def code_generation 
    @user = User.find(:first, :conditions => {:email => params[:email]})
    if @user
      @user.code = User.generate_code
      @user.save # HERE .SAVE IS RETURNING FALSE, SO I DONT HAVE IT IN DATABASE
    end
end

问题是@ user.save返回false并且数据库中的代码没有改变,我做错了什么?谢谢。

编辑:此行@user.code = User.generate_code有效,我可以通过调试来检查。

0 个答案:

没有答案