2016-08-02 29 views
1

Ich versuche, in einem Formular eine verschachtelte Form gehen zwei Ebenen tief. Es geht darum, eine Fabrik zu schaffen. Sowohl die erste als auch die zweite Ebene sind collection_check_boxes, um ausgesäte Objekte auszuwählen.Verschachtelte Form zwei Ebenen tief in Rails für die Auswahl der gesetzten Objekte

Relations in Worte

Zum ersten Ebene hatte ich es zu arbeiten und konkret das sieht für einen Moment wie:
A Fabrik viele Maschinen hat, durch behandelt.

Dann wollte ich eine Verbindung zu den Maschinen in der gleichen Form hinzuzufügen:
A Maschine hat viele Materialien, durch Feeds.

Ein Fabrikmodell wie folgt aussieht:

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 

Und das Maschinenmodell logisch daraus abgeleitet wird, aber ohne die verschachtelten Attribute für Materialien natürlich. (Material ist ein Endpunkt hier.)

Dann wird das Steuerteil für die Form, die Fabrik zu erstellen (factory_controller.rb):

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

@materials gibt es auch in den entsprechenden Maßnahmen.

und die Form wie folgt aussieht:

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

Sorry für alle die Super irrelevant CSS.

Meine Spezifikation sagt:

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> 

Antwort

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>