Techioz Blog

Rails 7 でポリモーフィック アソシエーションを使用した許可されていないパラメータ

概要

上の画像に基づいて、簡単な例を実行します。

モデル: 人

class Person < ApplicationRecord
  belongs_to :personable, polymorphic: true
end

モデル: 顧客

class Customer < ApplicationRecord
  has_one :person, as: :personable
  accepts_nested_attributes_for :person
end

コントローラー: customer_controller

def new
  @customer = Customer.new
  @customer.build_person
end

def create
  @customer = Customer.new(customer_params)
  @customer.save
  redirect_to customers_path
end

private

def customer_params
  params.require(:customer).permit(:id, person_attributes: [:id, :name, :personable_type, :personable_id])
end

ビュー

<%= form_with(model: customer) do |form| %>
  <%= form.fields_for customer.person do |form_fields| %>
    <%= form_fields.label :name %>
    <%= form_fields.text_field :name %>
  <% end %>
  <div>
    <%= form.submit %>
  </div>
<% end %>

Rails Console を使用して実行すると、以下のコードによれば問題ありません。

c = Customer.create()
Person.create(name: "Saulo", personable: c)

しかし、ビューとコントローラーを使用して実行すると、以下のエラーが発生します。

Unpermitted parameter: :person. Context: { controller: CustomersController, action: create, request: #<ActionDispatch::Request:0x00007fdad45e3650>, params: {"authenticity_token"=>"[FILTERED]", "customer"=>{"person"=>{"name"=>"Alisson"}}, "commit"=>"Create Customer", "controller"=>"customers", "action"=>"create"} }

このエラーは customer_params メソッドにあると思いますが、解決する方法が見つかりませんでした。

解決策

Rails は person 属性が person_attributes の下にネストされることを期待していますが、フォームは代わりに person の下にそれらを送信しています。

これを修正するには、fields_for がフォーム内の person_attributes の下にフィールドをネストするように正しく設定していることを確認します。

<%= form_with(model: [customer, customer.build_person]) do |form| %>
  <%= form.fields_for :person_attributes, customer.person do |person_form| %>
    <%= person_form.label :name %>
    <%= person_form.text_field :name %>
  <% end %>
  <%= form.submit %>
<% end %>

これにより、ネストされた属性の正しいパラメーター名 (person_attributes) が生成されるはずです。