2016-08-02 7 views
1

の入れ子になったフォームを作成しようとしています。それは工場を作ることです。第1レベルと第2レベルの両方とも、シードオブジェクトを選択するためのcollection_check_boxesです。 を処理して、
工場マシン多くを持っている:私はそれを具体的にその瞬時にしていた最初のレベルに単語
シードオブジェクトを選択するための2レベルの深さのネストされたフォーム

関係は次のようになります。

は、それから私は、同じフォーム内のマシンに関連付けを追加したい:
を供給してマシンは、多くの材料を持っています。

工場モデルは次のようになります。

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はまた、関連するアクションに存在します。すべてのスーパー無関係なCSSの申し訳ありません

<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> 

は、フォームは次のようになります。

私のスペックは言う:

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> 
関連する問題