我遇到了以下问题。我有一个名为 user 的模型,它有一个名为 activate 的列。我试图在激活方法的情况下更新该值?,但它给了我错误:验证失败:密码不能为空,密码太短(最少为 6 个字符)这对我来说没有意义,因为我没有接触密码字段!我只想更新已激活的列。我把我认为相关的代码放在这里,但如果您认为您需要更多,请询问:) 非常感谢您提前!
模型:
attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation, :activated
has_many :sucu_votes
email_regex = /\A[\w+\-.]+@[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