繁体   English   中英

在Rails中嵌套两层深度,用于选择种子对象

[英]Nested form two levels deep in Rails for selecting seeded objects

我试图建立一个嵌套形式的形式,深入两层 这是关于创建一个工厂。 第一级和第二级都是collection_check_boxes用于选择种子对象。

言语关系

到了第一级,我让它工作,具体地说,它暂时看起来像:
工厂有许多机器 ,通过把手

然后我想以相同的形式为机器添加关联:
机器有许多材料 ,通过饲料

工厂模型看起来像:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM