5
我遇到了以下問題。我有一個名爲user的模型,它有一個名爲activated的列。我試圖更新該值的方法激活?,但它給了我錯誤:驗證失敗:密碼不能爲空,密碼太短(最少6個字符)這對我沒有意義,因爲我沒有觸摸密碼字段!我只想更新激活的列。我把這裏的代碼放在這裏我認爲它相關,但如果你認爲你需要更多的只是問:) 非常感謝你提前!Rails update_attribute
型號:
attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation, :activated
has_many :sucu_votes
email_regex = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
validates :name, :presence => true,
:length => { :maximum => 50 }
validates :email, :presence => true,
:format => {:with => email_regex},
:uniqueness => { :case_sensitive => false }
validates :password, :presence => true,
:length => { :within => 6..15 },
:confirmation => true
before_save :encrypt_password
def activated?
self.update_attributes!(:activated => true)
return self.activated
end
控制器從該方法激活?被稱爲
def activate
if request.get?
user=User.find_by_id(params[:id])
if user.activated?
flash[:notice]="Your account has been activated"
#redirect_to :controller => 'sessions', :action => 'new'
else
flash[:error]="We couldnt activate the account"
redirect_to :controller => 'sessions', :action => 'new'
end
end
end
非常感謝您!我之前確實有這樣的感覺,但是由於另一個原因,它並沒有工作。但它的一切都好了:) – gumlym 2012-03-27 09:55:13