【问题标题】:rails multi-level nested forms with cocoon and tablesrails 带有茧和表格的多级嵌套表单
【发布时间】:2017-11-10 12:24:53
【问题描述】:

我已经成功地使用 Cocoon 和表格实现了一级嵌套表单。但是,我很难将注意力集中在如何做另一个嵌套级别上。我的问题是如何用桌子做到这一点。也许一张桌子根本不是写的方式。感谢您帮助新手。

这是我的模型:

class Profession < ApplicationRecord
  has_many :procedure_categories, dependent: :destroy
  accepts_nested_attributes_for :procedure_categories, allow_destroy: true
end

还有:

class ProcedureCategory < ApplicationRecord
  belongs_to :profession
  has_many :procedures

  accepts_nested_attributes_for :procedures, allow_destroy: true
end

还有:

class Procedure < ApplicationRecord
  belongs_to :procedure_category
end

这是我的顶级表单代码:

<%= form_for(@profession) do |f| %>
  <%= render 'shared/profession_error_messages' %>

  <%= f.label :profession %>
  <%= f.text_field :profession, class: 'form-control'  %>

  <%= f.label :description %>
  <%= f.text_field :description, class: 'form-control'  %>

  <%= f.label :active, class: "checkbox inline" do %>
    <%= f.check_box :active %>
    <span>Active profession?</span>
  <% end %>

  <table class='table'>
    <thead>
      <tr>
        <th>Category</th>
        <th>Description</th>
        <th>Display Order</th>
        <th>Selection Type</th>
        <th>Delete</th>
        <th>Edit</th>
      </tr>
    </thead>
    <tbody class="categories">
      <%= f.fields_for :procedure_categories do |procedure_category| %>
        <%= render 'procedure_category_fields', f: procedure_category %>
      <% end %>
    </tbody>
  </table>


  <%= link_to_add_association 'Add Category', f, :procedure_categories, 
    data: { association_insertion_node: '.categories', association_insertion_method: :append } %>

  <br><br>    
  <%= f.submit "Save", class: "btn btn-primary" %>
  <% end %>

还有下一层:

<tr class="nested-fields">
  <td><%= f.text_field :category, class: 'form-control' %></td>
  <td><%= f.text_field :description, class: 'form-control' %></td>
  <td><%= f.text_field :display_order, class: 'form-control' %></td>
    <% cs = options_for_select(controls, f.object.selection_type) %>
  <td><%= f.select :selection_type, cs, class: 'form-control' %></td>
  <td><%= link_to_remove_association "Remove Category", f %></td>
  <% if f.object != nil %>
    <td><%= link_to "Category", edit_procedure_category_path(@profession,f.object) %><td></td>
  <% end %>
</tr>

所以,我正在为如何实现嵌套的最终级别(程序)而苦苦挣扎。

感谢您的收听。

【问题讨论】:

标签: ruby-on-rails nested attributes multi-level apache-cocoon


【解决方案1】:

使用has_many :through

这是我的模型:

class Profession < ApplicationRecord
  has_many :procedures, through: categories
  has_many :categories, dependent: :destroy
  accepts_nested_attributes_for :categories, allow_destroy: true
end

category 中重命名此procedure_category 模型

class Category < ApplicationRecord
  belongs_to :profession
  has_many :procedures

  accepts_nested_attributes_for :procedures, allow_destroy: true
end

还有:

class Procedure < ApplicationRecord
  belongs_to :category
end

如果我错过了什么,您可以查看instruction from the rails guide

控制器professions#new 操作应创建以下变量,以便它们在视图中可用:

def new
    @profession = Profession.new
    @categories = @profession.categories.build
    @procedures = @categories.procedures.build
end

视图使用该变量,因此存储用户输入并在/profession/ 发出post 请求,这些输入存储在parameters 哈希中

<%= form_for(@profession) do |f| %>
      <%= f.fields_for :categories do |category| %>
               <%= category.fields_for :procedures do |precedure| %>
               <% end %>
      <% end %>
<% end %>

fields_for 生成一个表单构建器。参数的名称将是 accept_nested_attributes_for 所期望的。例如,创建具有 2 个地址的用户时,提交的参数如下所示:

这就是您的parameters 的样子:

{
  'profession' => {
    'name' => 'John Doe',
    'categories_attributes' => {
      '0' => {
        'kind' => 'Home',
        'street' => '221b Baker Street',
        'procedures_attributes' => {
         '0' => {},
         '1' => {}
         }
      },
      '1' => {
        'kind' => 'Office',
        'street' => '31 Spooner Street'
      }
    }
  }
}

所以请确保您的表单指向post url /professions/,并且路由将触发professions#create 操作

def create
    binding.pry
end

如有任何问题,请设置 binding pry 并在您的控制台中检查您的 parameters 是如何显示的。

阅读更多 http://guides.rubyonrails.org/form_helpers.html#building-complex-forms

【讨论】:

  • 感谢您的详细回答,但我不太明白您为什么提到使用 through: 来表示关系。我没有尝试实现多对多关系。这对我有什么帮助?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多