Rails 3 – 设计:如何在编辑注册时跳过’current_password’?

我用我的设计模型实现了omniauth,所以我可以使用其他服务进行身份validation。 我的模型不再需要密码,因为用户可以使用twitter,facebook进行身份validation…

一切正常,但是,当用户尝试编辑其注册时,设计会跳过该过程,因为用户没有通知’current_password’(现在在某些情况下不存在)。

我创建了一个注册控制器来覆盖一个设备:

class RegistrationsController < Devise::RegistrationsController def update super end end 

但是我没有找到任何关于如何跳过密码validation的文档,我怎样才能在更新操作中执行此操作?

与上面类似,请尝试将其放入您的用户模型中:

 # bypasses Devise's requirement to re-enter current password to edit def update_with_password(params={}) if params[:password].blank? params.delete(:password) params.delete(:password_confirmation) if params[:password_confirmation].blank? end update_attributes(params) end 

以下对我有用:

在我的用户控制器中,在更新操作中,我有

 params[:user].delete(:password) if params[:user][:password].blank? params[:user].delete(:password_confirmation) if params[:user][:password_confirmation].blank? 

也许您可以将其改编为before_save回调?

即使答案已经存在了一段时间我想发布一个新答案,因为我认为所选答案有一点缺陷。 也许它现在没有创建答案,但现在在2013 ,答案将是这样的:

解决方案是在User模型中创建如下:

  # bypass re-entering current password for edit def update_with_password(params={}) current_password = params.delete(:current_password) if params[:password].blank? params.delete(:password) params.delete(:password_confirmation) if params[:password_confirmation].blank? end update_attributes(params) clean_up_passwords end 

有一个更简单的答案,我不知道什么时候设计首先有这种方法,但只是添加

Model.update_without_password(params)

它将更新属性而无需当前密码。