本文介绍了如何指定devise_parameter_sanitizer进行编辑操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将Devise添加到Rails 4应用程序,并成功地将用户名等添加到了我的用户模型中。此外,我可以使用懒惰的方式和交易来存储这些字段,即

I've added Devise to my Rails 4 application, and successfully added username etc. to my User model. Furthermore, I'm able to store those fields using the lazy way™, i.e.

class ApplicationController < ActionController::Base
  before_filter :configure_permitted_parameters, if: :devise_controller?

  protected

    def configure_permitted_parameters
      devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :firstname, :middlename, :lastname) } 
    end
end

但是,我尝试

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :firstname, :middlename, :lastname) }
  devise_parameter_sanitizer.for(:edit) { |u| u.permit(:email, :password, :password_confirmation, :firstname, :middlename, :lastname) }
end

但是没有按预期方式工作(在编辑操作调用时用户名不存储)。还有什么我需要做的才能让它工作吗?谢谢!

but that didn't work quite as expected (username not being stored when invoked by the edit action). Is there something else I need to do in order to get that to work? Thanks!

推荐答案

再次,这是一个阅读手册的问题...

Once again, it was a matter of reading the manual ...

魔术词是:account_update ,因此工作版本变为

The magic word is :account_update and thus the working version becomes

def configure_permitted_parameters
  devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:email, :password, :password_confirmation, :firstname, :middlename, :lastname, :nickname) }
  devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:email, :password, :password_confirmation, :current_password, :firstname, :middlename, :lastname, :nickname) }
end

请注意,如果您使用非标准参数登录,您要查找的字是:sign_in (如预期)。

Note that if you're in the business of signing in using non-standard parameters, the word you're looking for is :sign_in (as expected).

这篇关于如何指定devise_parameter_sanitizer进行编辑操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 08:20