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

Tia*_*ago 12 ruby-on-rails devise omniauth ruby-on-rails-3

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

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

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

class RegistrationsController < Devise::RegistrationsController
  def update
    super
  end
end
Run Code Online (Sandbox Code Playgroud)

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

rxb*_*rxb 34

与上面类似,请尝试将其添加到您的用户模型中:

# 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
Run Code Online (Sandbox Code Playgroud)

  • 添加对`clean_up_passwords`的调用可能是值得的.保存后,它将`password`和`password_confirmation`属性设置为nil,以保密用户的密码.请参阅Devise的[update_with_password实现](https://github.com/plataformatec/devise/blob/master/lib/devise/models/database_authenticatable.rb#L56)以供参考. (4认同)

Dav*_*ulc 10

以下对我有用:

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

params[:user].delete(:password) if params[:user][:password].blank?
params[:user].delete(:password_confirmation) if params[:user][:password_confirmation].blank?
Run Code Online (Sandbox Code Playgroud)

也许你可以适应before_save回调?