2016-08-02 50 views
1

我試圖在表單中構建一個兩層深度的嵌套窗體。這是關於創建一個工廠。第一級和第二級都是collection_check_boxes來選擇種子對象。選擇種子對象的Rails嵌套窗體中的兩級深度

關係的話

爲第一級我是有工作的和具體的體現是暫時的樣子:
一個工廠有許多,通過處理

然後,我想一個協會以相同的形式添加到機器:
一個有許多材料,通過飼料

甲工廠模型看起來像:

validates :name, presence: true 
    validates :description, presence: true 
    # Factory to handle machines. 
    has_many :handles, :dependent => :destroy 
    has_many :machines, :through => :handles 
    # Factory needs to know about materials (fed through machines). 
    accepts_nested_attributes_for :machine 

而且該機模型邏輯地源於此,但沒有對課程材料的嵌套屬性。 (材料是端點在這裏。)

然後窗體的控制器部分創建工廠(factory_controller.rb):

def factory_params 
    params.require(:factory).permit(:name, :description, 
     :machine_ids => [], machines: [:material_ids => [] ]) 
    end 

@materials也存在於相關行動。

和形式如下:

<div class="w3-row"> 
    <div class="w3-twothird" style="margin-left: 16.65%"> 

     <%= simple_form_for @factory do |f| %> 

     <!-- Input --> 
     <%= f.input_field :name %> 
     <%= f.label :name %> 
     <%= f.error :name %> 
     <%= f.input_field :description, rows: 7 %> 
     <%= f.label :description %> 
     <%= f.error :description %><br><br> 

     <div class="w3-row w3-margin-top"> 
      <!-- Machines card --> 
      <div class="w3-third w3-card w3-padding-bottom"> 
      <h5 class="w3-text-teal w3-center">Machines</h5> 
      <ul class="w3-ul" id="machines"> 
      <%= f.collection_check_boxes :machine_ids, @machines, :id, :name do |b| %> 
       <li> 
       <%= b.label do %> 
        <%= b.check_box class: "w3-check" %> 
        <%= b.text %> 
       <% end %> 
       </li> 
      <% end %> 
      </ul> 
      </div> 
      <!-- Materials card --> 
      <div class="w3-third w3-card w3-padding-bottom"> 
      <h5 class="w3-text-teal w3-center">Machines</h5> 
      <ul class="w3-ul" id="materials"> 
      <%= f.collection_check_boxes :material_ids, @materials, :id, :sort do |b| %> 
       <li> 
       <%= b.label do %> 
        <%= b.check_box class: "w3-check" %> 
        <%= b.text %> 
       <% end %> 
       </li> 
      <% end %> 
      </ul> 
      </div> 

     </div> 

     <br><br> 
     <!-- Zenden --> 
     <div class="w3-center w3-margin-bottom"> 
      <%= f.button :button, class: "w3-btn w3-blue w3-center" %> 
     </div> 

     <% end %> 

    </div> 
</div> 

對不起,所有的超級無關的CSS。

我的規範說:

Users can create new factory with associated materials on the associated machines 
    Failure/Error: <%= b.check_box class: "w3-check" %> 

    ActionView::Template::Error: 
     undefined method `material_ids' for #<Factory:0x007fb3f41fbad0> 

回答

0
You should use fields_for method to manage associated fields in a form, 

try below code : 

<div class="w3-row"> 
    <div class="w3-twothird" style="margin-left: 16.65%"> 

     <%= simple_form_for @factory do |f| %> 

     <!-- Input --> 
     <%= f.input_field :name %> 
     <%= f.label :name %> 
     <%= f.error :name %> 
     <%= f.input_field :description, rows: 7 %> 
     <%= f.label :description %> 
     <%= f.error :description %><br><br> 

     <div class="w3-row w3-margin-top"> 
      <!-- Machines card --> 
      <div class="w3-third w3-card w3-padding-bottom"> 
      <h5 class="w3-text-teal w3-center">Machines</h5> 
      <ul class="w3-ul" id="machines"> 
      <%= f.collection_check_boxes :machine_ids, @machines, :id, :name do |b| %> 
       <li> 
       <%= b.label do %> 
        <%= b.check_box class: "w3-check" %> 
        <%= b.text %> 
       <% end %> 
       </li> 
      <% end %> 
      </ul> 
      </div> 
      <!-- Materials card --> 
      <% f.fields_for @machines do |ff| %> 
      <div class="w3-third w3-card w3-padding-bottom"> 
       <h5 class="w3-text-teal w3-center">Machines</h5> 
       <ul class="w3-ul" id="materials"> 
       <%= ff.collection_check_boxes :material_ids, @materials, :id, :sort do |b| %> 
       <li> 
        <%= b.label do %> 
        <%= b.check_box class: "w3-check" %> 
        <%= b.text %> 
        <% end %> 
       </li> 
       <% end %> 
       </ul> 
      </div> 
      <% end %> 

     </div> 

     <br><br> 
     <!-- Zenden --> 
     <div class="w3-center w3-margin-bottom"> 
      <%= f.button :button, class: "w3-btn w3-blue w3-center" %> 
     </div> 

     <% end %> 

    </div> 
</div>