0

我无法弄清楚这个是怎么回事。我accepts_nested_attributes_for在我的模型中使用,只要 ids 在参数、数组中,这个问题就会发生在create

没有将 String 隐式转换为 Integer

福型号:

has_many :foo_perks, dependent: :destroy
has_many :perks, through: :foo_perks
accepts_nested_attributes_for :foo_perks, :another_one, allow_destroy: true

Foo 控制器

# create
Foo.create(foo_params) # Issue here

# private
def foo_params
  params.require(:foo).permit(foo_perks_attributes: [ perk_id: [] ]) # omitted title and other
end

很标准的东西。风景:

[..] # form_for(..)
<%= f.fields_for :foo_perks_attributes do |fp| %>
  <% @perks.each do |perk| %>
    <%= fp.check_box(:perk_id, { multiple: true }, perk.id, nil) %>
  <% end %>
<% end %>
<% end %> # end of form_for

提交的参数示例:

"foo"=>{"title"=>"", "foo_perks_attributes"=>{"perk_id"=>["4"]}}

foo_perks当我在控制台中手动创建 a时,我没有看到任何问题,也没有错误。我对许可证做错了吗?

Foo没有传递 id 时创建成功。

4

0 回答 0