如何为编辑操作指定 devise_parameter_sanitizer? [英] How to specify devise_parameter_sanitizer for edit action?

查看:32
本文介绍了如何为编辑操作指定 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆