Ruby on rails 嵌套属性和不允许的参数

Ruby on rails 嵌套属性和不允许的参数,ruby-on-rails,ruby-on-rails-4,nested-attributes,Ruby On Rails,Ruby On Rails 4,Nested Attributes,我有一个表单正在传递嵌套属性,它对“Child”有效,但“books”似乎没有保存 我有三种型号。每个孩子可以有两本书: class Child < ActiveRecord::Base has_many :hires has_many :books, through: :hires end class Hire < ActiveRecord::Base belongs_to :book belongs_to :child accepts_nested_attribute

我有一个表单正在传递嵌套属性,它对“Child”有效,但“books”似乎没有保存

我有三种型号。每个孩子可以有两本书:

class Child < ActiveRecord::Base
 has_many :hires
 has_many :books, through: :hires
end

class Hire < ActiveRecord::Base
 belongs_to :book
 belongs_to :child
 accepts_nested_attributes_for :book
 accepts_nested_attributes_for :child
end

class Book < ActiveRecord::Base
  has_many :hires
  has_many :children, through: :hires
  belongs_to :genres
end
我搞不懂为什么我会得到未经许可的参数错误?有什么建议吗

*编辑-包括视图,因为我现在怀疑这可能会起作用*

<%= form_for(@hire) do |f| %>

<%= f.select(:child_id, Child.all.collect {|a| [a.nickname, a.id]}) -%>
<%= f.label :child_id %><br>

<%= f.fields_for :books do |books_form| %>
<%= books_form.label :book_id %><br>
<%= books_form.select(:book_id, Book.all.collect {|a| [a.Title, a.id]}) -%>
<%= books_form.label :book_id %><br>
<%= books_form.select(:book_id, Book.all.collect {|a| [a.Title, a.id]}) -%>
<% end %>


<div class="actions">
<%= f.submit %>
</div>
<% end %>





您需要使用以下各项:

#app/controllers/hires_controller.rb
class HiresController < ApplicationController
   def new
      @hire = Hire.new
      2.times do
         @hire.build_book
      end
   end
end
。。。应该是

2.times { @hire.build_book }

当您使用单数关联时,您必须以单数形式构建关联对象:
build\u object
,而使用复数时,您可以使用
复数.build
调用。

Hi Rich,谢谢。我想你是对的。某些东西似乎正在影响属性的构建。我将控制器代码更改为
2.times do@hire.build\u book end
,但仍然存在相同的问题。还有其他想法吗?我想知道这是否与我的联想有关。我说的是“接受:book的嵌套属性”,但我的
f.字段
是用于
书籍
,我的属性是用于
书籍
?去买些食物,然后看看
#app/controllers/hires_controller.rb
class HiresController < ApplicationController
   def new
      @hire = Hire.new
      2.times do
         @hire.build_book
      end
   end
end
2.times { @hire.books.build }
2.times { @hire.build_book }