【问题标题】:Strong Parameters for nested omniauth嵌套omniauth的强参数
【发布时间】:2016-06-10 13:52:01
【问题描述】:

我有一个使用 Devise 3.5.10 的 Rails 4.0.13 应用程序。我的 User 模型是 :omniauthable 使用 嵌套 authentications has_many 关系,因此用户可以通过多个提供者进行 Omniauth:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, :confirmable,
         :recoverable, :rememberable, :trackable,
         :omniauthable, :omniauth_providers => Authentication.auth_methods

  has_many :authentications, dependent: :destroy
  accepts_nested_attributes_for :authentications
end

(我最初在 Rails 3.2 上实现了这一点,所以我不记得我必须做出的确切更改才能使其工作。我不相信它是相关的,但如果有必要可以尝试看看)。

这意味着使用 Omniauth 的用户注册具有如下参数:

Parameters: {"utf8"=>"✓", "user"=>{
  "authentications_attributes"=>
  {"0"=>{"provider"=>"open_id",
         "uid"=>"http://pretend.openid.example.com?id=12345",
         "nickname"=>"http://pretend.openid.example.com"}},
  "name"=>"Person1",
  "email"=>"Person1@example.com", "password"=>"[FILTERED]",
  "password_confirmation"=>"[FILTERED]"}, "commit"=>"Sign up"}

我无法弄清楚如何获得强大的参数来允许这样做。我目前的尝试是params.require(:user).permit(:name, :email, :password, :password_confirmation, authentications_attributes: {"0" =&gt; [:provider, :uid, :nickname]}),但这仍然会产生Unpermitted parameters: provider, uid, nickname 的日志。

我怎样才能允许这些参数?

【问题讨论】:

    标签: ruby-on-rails devise omniauth strong-parameters


    【解决方案1】:

    指定嵌套参数时,XYZ_attributes 部分采用数组,而不是哈希。

    你的情况,试试

    authentications_attributes: [:provider, :uid, :nickname]

    在

    params.require(:user).permit(:name, :email, :password, :password_confirmation, authentications_attributes: [:provider, :uid, :nickname])
    

    来源:https://github.com/rails/strong_parameters#nested-parameters

    【讨论】:

    • 天啊!我对“0”如此着迷,以至于我没有想到这将如何与has_many 交互。谢谢,太完美了。
    猜你喜欢
    • 2015-09-20
    • 1970-01-01
    • 2018-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-28
    • 1970-01-01
    相关资源
    最近更新 更多