设计和强参数 [英] Devise and Strong Parameters

查看:66
本文介绍了设计和强参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何集成这两个宝石(devise + Strong Parameters),因为强大的参数可能会被添加到rails的核心4.0

I would like to know how to integrate both of this gems(devise + Strong Parameters), since strong params will likely be added to the rails core in 4.0

任何帮助欢迎
谢谢

any help is welcome thanks

推荐答案

更新for devise 4.x

Update for devise 4.x

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

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.permit(:sign_up, keys: [:username])
    devise_parameter_sanitizer.permit(:sign_in, keys: [:username])
    devise_parameter_sanitizer.permit(:account_update, keys: [:username])
  end
end






添加两个宝石后,设计将正常工作。


After adding both gems, devise will work as normal.

更新:使用最新版本的Devise 3.x,如设计#强参数所述,验证密钥(通常为电子邮件字段)和密码字段已被允许。但是,如果注册表上有其他字段,您需要让Devise知道允许的额外字段。最简单的方法是使用过滤器:

Update: With the latest version of Devise 3.x, as described at devise#strong-parameters, the authentication key (normally the email field), and the password fields are already permitted. However, if there are any additional fields on the signup form, you will need to let Devise know the extra fields to permit. The easiest way to do this is with a filter:

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

  protected

  def configure_permitted_parameters
    devise_parameter_sanitizer.for(:sign_up) << :username
  end
end

对于Devise 2.x,如果您使用需要在用户模型中明确列入污染参数的安全功能:

For Devise 2.x, if you use the safety feature requiring explicitly whitelisting tainted parameters in the user model:

include ActiveModel::ForbiddenAttributesProtection

需要的更改可以在 https://gist.github.com/3350730 ,它覆盖了一些控制器。

the changes needed are found at https://gist.github.com/3350730 which overrides some of the controllers.

这篇关于设计和强参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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