三重嵌套形式 Rails 5.1 中不允许的参数

Unpermitted parameter on triple nested form Rails 5.1

我有以下模型的三重嵌套形式。

roastscountriesregions

我可以创建一个新的 roast,它也创建了国家,但不是地区。控制台输出:

  Parameters: {"utf8"=>"✓", "authenticity_token"=>"cR5jRFbaslQU5+nWiU8rBWlC9OQ0E9zgMwY1YCF33Z1cwPkmJvsO5GKQ4hTNIB4Mku3EuL19WJTrg4e03gHW4Q==", "roast"=>{"roaster"=>"Square Mile", "name"=>"Red Brick", "countries_attributes"=>{"0"=>{"country_name"=>"UK"}}, "regions"=>{"region_name"=>"Midlands"}, "bestfor"=>"", "roast"=>"", "tastingnotes"=>""}, "commit"=>"Create Roast"}
  User Load (0.7ms)  SELECT  "users".* FROM "users" WHERE "users"."user_id" =  ORDER BY "users"."user_id" ASC LIMIT   [["user_id", 15], ["LIMIT", 1]]
Unpermitted parameter: :regions

我注意到,对于 countries,日志有 "countries_attributes",但 regions 只是 "regions",而这也应该是 regions_attributes。我在国家/地区控制器中嵌套了国家/地区参数中的区域参数。那是对的吗?我试过它们没有嵌套,但也不起作用:

我的参数:

def roast_params
  params.require(:roast).permit(:roaster, :name, :bestfor, :beans, :roast, :tastingnotes, :notes, :slug, :avatar, countries_attributes: [:country_id, :country_name, regions_attributes: [:id, :region_name]])
end

将其更改为 regions_attributes: [:region_id, :region_name] 没有帮助。

roast.rb

class Roast < ApplicationRecord
  has_many :tastings
  has_many :countries
  has_many :notes, through: :tastings
  has_many :comments, as: :commentable
  accepts_nested_attributes_for :countries

country.rb

class Country < ApplicationRecord
  has_many :regions, inverse_of: :country
  accepts_nested_attributes_for :regions
  belongs_to :roasts

region.rb

class Region < ApplicationRecord
  belongs_to :country, inverse_of: :regions

烘烤控制器

  def new
    @roast = Roast.new
    @roast.countries.build.regions.build
  end

嵌套字段的表单

  <div class="form-group">
    <%= form.fields_for :countries do |countries_form| %>
      <%= countries_form.label :country %>
      <%= countries_form.text_field :country_name, class: "form-control" %>
<br />
      <%= form.fields_for :regions do |regions_form| %>
        <%= regions_form.label :region %>
        <%= regions_form.text_field :region_name, class: "form-control" %>
      <% end %>
    <% end %>
  </div>

我觉得

<%= form.fields_for :regions do |regions_form| %>
    <%= regions_form.label :region %>
    <%= regions_form.text_field :region_name, class: "form-control" %>
<% end %>

应该是

<%= countries_form.fields_for :regions do |regions_form| %>
    <%= regions_form.label :region %>
    <%= regions_form.text_field :region_name, class: "form-control" %>
<% end %>