嵌套 omniauth 的强参数

Strong Parameters for nested omniauth

我有一个使用 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" => [:provider, :uid, :nickname]}),但仍然会生成 Unpermitted parameters: provider, uid, nickname.

的日志

如何允许这些参数?

指定嵌套参数时,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