我的用户模型无法保存,因为password和password_confirmation不能为空。这种情况发生在:
如果使用password_required,
validates_presence_of :password, :if => :password_required?
def password_required?
!persisted? || !password.nil? || !password_confirmation.nil?
end
这是我的rails控制台输出:
Loading development environment (Rails 3.2.11)
1.9.3p374 :001 > u = User.last
User Load (1.0ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
=> #<User id: 9, name: "Name", email: "name@example.com", encrypted_password: "$2a$10$oCL2K0nEa8LxaMGuC1oyNOuwIHk8l/IOW.AylGcWTYj3...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 2, current_sign_in_at: "2013-02-06 20:56:14", last_sign_in_at: "2013-02-06 20:52:01", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", confirmation_token: nil, confirmed_at: "2013-02-07 22:04:01", confirmation_sent_at: "2013-02-07 22:03:25", unconfirmed_email: nil, authentication_token: nil, created_at: "2013-02-06 20:52:00", updated_at: "2013-02-07 22:04:31">
1.9.3p374 :002 > u.save
(0.2ms) BEGIN
(0.2ms) ROLLBACK
=> false
1.9.3p374 :003 > u.errors
=> #<ActiveModel::Errors:0x007f832c04f090 @base=#<User id: 9, name: "Name", email: "name@example.com", encrypted_password: "$2a$10$oCL2K0nEa8LxaMGuC1oyNOuwIHk8l/IOW.AylGcWTYj3...", reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 2, current_sign_in_at: "2013-02-06 20:56:14", last_sign_in_at: "2013-02-06 20:52:01", current_sign_in_ip: "127.0.0.1", last_sign_in_ip: "127.0.0.1", confirmation_token: nil, confirmed_at: "2013-02-07 22:04:01", confirmation_sent_at: "2013-02-07 22:03:25", unconfirmed_email: nil, authentication_token: nil, created_at: "2013-02-06 20:52:00", updated_at: "2013-02-07 22:04:31">, @messages={:password=>["can't be blank"], :password_confirmation=>["can't be blank"]}>
1.9.3p374 :004 > !u.persisted? || !u.password.nil? || !u.password_confirmation.nil?
=> false
感谢您的帮助!
答案 0 :(得分:4)
感谢@rainkinz,我回到了基础(用户模型)并仔细研究了一下。我发现我导入的模块正在添加一个额外的
validates :email, :password, :password_confirmation, presence: true
我能够通过在控制台中运行来看到这一点:
User.validators
...
#<ActiveModel::Validations::PresenceValidator:0x007fb80fbcd260
@attributes=[:email, :password, :password_confirmation],
@options={}>,
...
这与设计放入的验证不同,其中包含:
@options={:if=>:password_required?}>,
删除那些解决了问题。谢谢你的帮助!
答案 1 :(得分:2)
在更新方法中执行此操作。
user = User.find_by_id(params[:id])
unless user.blank?
if user.update_attributes(params[:user])
flash[:notice] = "User updated successfully."
redirect_to "somwhere"
else
render :action => 'edit'
end
else
render :action => 'edit'
end
如果您不想更新旧密码,请在更新前添加这些行,以便新代码为:
user = User.find_by_id(params[:id])
unless user.blank?
params[:user].delete(:password) if params[:user][:password].blank?
params[:user].delete(:password_confirmation) if params[:user][:password_confirmation].blank?
if user.update_attributes(params[:user])
flash[:notice] = "User updated successfully."
redirect_to "somwhere"
else
render :action => 'edit'
end
else
render :action => 'edit'
end
在user.rb模型中写下这样的东西
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable, :locakable
答案 2 :(得分:0)
我遇到了类似的问题,但是对我有用的解决方案是在完成我想做的事情之后,再次使用super
调用devise的方法。例如:
def password_required?
return false if some_check?
super
end
此other answer的信用使我眼前一亮。