如何为编辑操作指定devise_parameter_sanitizer?

con*_*tor 50 devise strong-parameters ruby-on-rails-4

我已将Devise添加到我的Rails 4应用程序中,并成功将用户名等添加到我的用户模型中.此外,我能够使用lazy way™存储这些字段,即

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

但是,我试过了

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

但是这并没有像预期的那样工作(编辑动作调用时没有存储用户名).为了让它发挥作用,我还需要做些什么吗?谢谢!

con*_*tor 85

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

神奇的词是:account_update因此工作版本变成了

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

请注意,如果您正在使用非标准参数进行登录,那么您正在寻找的单词:sign_in(正如预期的那样).


Mir*_*318 55

对于Devise 4.1+

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

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:first_name, :last_name, :email])
    devise_parameter_sanitizer.permit(:account_update, keys: [:first_name, :last_name, :phone, :email, bank_attributes: [:bank_name, :bank_account]])
  end
end
Run Code Online (Sandbox Code Playgroud)

.for方法已弃用,现在我们使用.permit

第一个arg是动作名称.:sign_up用于创建新的Devise资源(例如用户),:account_update用于编辑/更新资源.

第二个arg :keys包含您允许的参数数组.

如果你愿意nested_attributes,有一个例子:account_update,你把一个单独的数组放在键中<object>_attributes.


tec*_*ags 15

@conciliator关于魔术词是正确的:account_update但这里是他提到的文档的链接http://rubydoc.info/github/plataformatec/devise/ 搜索'devise_parameter_sanitizer',你会看到以下内容:

Devise中只有三个操作允许将任何参数集传递给模型,因此需要进行清理.默认情况下,它们的名称和允许的参数为:

sign_in (Devise::SessionsController#new) - Permits only the authentication keys (like email)
sign_up (Devise::RegistrationsController#create) - Permits authentication keys plus password and password_confirmation
account_update (Devise::RegistrationsController#update) - Permits authentication keys plus password, password_confirmation and current_password
Run Code Online (Sandbox Code Playgroud)


小智 6

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

  • 这个":account_update"就是我想要的.是否有所有这些行动的综合清单? (2认同)