'Build' 嵌套表单属性时出错

'Build' error when nesting form attributes

我在尝试创建新的嵌套表单时遇到错误。

我有 3 个模型:

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

class Hire < ActiveRecord::Base
 belongs_to :books
 belongs_to :children
 accepts_nested_attributes_for :books
 accepts_nested_attributes_for :children
end

class Book < ActiveRecord::Base
  has_many :hires
  has_many :children, through: :hires
  belongs_to :genres
end

我正在尝试设置一个允许儿童 'hire' 2 本书的视图。

视图如下所示:

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

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

  <%= f.fields_for :books do |books_form| %>

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

<%= f.submit %>
<% end %>

控制器看起来像这样:

class HiresController < ApplicationController

...    

 def new
     @hire = Hire.new
     2.times { @hire.books.build }
 end

 def create
    @hire = Hire.new(hire_params)

    respond_to do |format|
      if @hire.save
        format.html { redirect_to @hire, notice: 'Hire was successfully created.' }
        format.json { render :show, status: :created, location: @hire }
      else
        format.html { render :new }
        format.json { render json: @hire.errors, status: :unprocessable_entity }
      end
    end
  end

 ...    

private
    # Use callbacks to share common setup or constraints between actions.
    def set_hire
      @hire = Hire.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def hire_params
      params.require(:hire).permit(:book, :child, books_attributes: [:id, :book, :child, :_destroy])
    end
end

我遇到错误:

undefined method `build' for nil:NilClass

我觉得这是我遗漏的明显问题,但任何帮助都会很棒!

 belongs_to :books
 belongs_to :children

你需要将这些单数化。

 belongs_to :book
 belongs_to :child

另请参阅 rails 指南的第 2.4 节作为参考 "The has_many :through Association"http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association

请记住,您在 Hire 对象上使用 belongs_to。

注意使用 belongs_to 关联时添加的方法:

association(force_reload = false)
association=(associate)
build_association(attributes = {})
create_association(attributes = {})
create_association!(attributes = {})

因此您应该能够在 Hire 对象上使用方法 build_book(例如 @hire.build_child 的 @hire.build_book)。

has_many协会新增的方法有:

collection(force_reload = false)
collection<<(object, ...)
collection.delete(object, ...)
collection.destroy(object, ...)
collection=(objects)
collection_singular_ids
collection_singular_ids=(ids)
collection.clear
collection.empty?
collection.size
collection.find(...)
collection.where(...)
collection.exists?(...)
collection.build(attributes = {}, ...)
collection.create(attributes = {})
collection.create!(attributes = {})

可用于您的 Book 和 Child 对象。例如:@book.childeren.build or @child.books.build.

参考 rails 指南第 4.2 和 4.3 节 'Methods Added by ..': http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association