繭のテーブルとテーブルで1レベルのネストされたフォームを正常に実装しました。しかし、私は別のネストされたレベルを行う方法に私の心をラップするのは難しい時を過ごしています。私の問題は、テーブルでこれを行う方法です。そして、テーブルはまったく行く書き込み方法ではないかもしれません。初心者を助けてくれてありがとう。ここで繭とテーブルを含む複数レベルのネストされたフォーム
私のモデルです:
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 %>
そして次の部分1レベル下:だから
<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>
、私は(手続き)をネストの最終レベルを実装する方法に苦しみました。
ありがとうございます。
class Profession < ApplicationRecord
has_many :procedures, through: categories
has_many :categories, dependent: :destroy
accepts_nested_attributes_for :categories, allow_destroy: true
end
がcategory
class Category < ApplicationRecord
belongs_to :profession
has_many :procedures
accepts_nested_attributes_for :procedures, allow_destroy: true
end
にこのprocedure_category
モデルの名前を変更し、::ここで使用has_many :through
で
読む詳細を確認しました、多分それは役立ちます:あなたの詳細な回答のためhttps://github.com/nathanvda/cocoon_simple_form_demo – nathanvda