'建立'嵌套表单属性时出错

时间:2015-11-05 19:17:06

标签: ruby-on-rails ruby-on-rails-4 build nested-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

我试图建立一个允许孩子们雇用的视图。 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

我觉得这很明显,我错过了,但任何帮助都会很棒!

1 个答案:

答案 0 :(得分:0)

 belongs_to :books
 belongs_to :children

你需要将这些单一化。

 belongs_to :book
 belongs_to :child

另请参阅导轨指南的第2.4节作为参考“has_many:通过关联”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_book @ hire.build_child)。

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.

有关参考,请参阅导轨指南4.2和4.3'方法添加..': http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association