ActiveAdmin has_many 有条件地显示部分内容
ActiveAdmin has_many display partial conditionally
我有一个 Table1 与 Table2.
有 has_many 关系
代码 1:-
## _edit_table1.html.erb
<% f.has_many :table2_relations, heading: false, new_record: true, allow_destroy: true do |r| %>
<%= render partial: "admin/table1/show/form_table2", locals: { r: r } %>
<% end %>
我想根据 Table2 的属性 0 显示部分,这是一个布尔值。仅当 attribute0 为真时,我才想显示部分。
我试过了:
代码 2:-
## _edit_table1.html.erb
<% f.has_many :table2_relations, heading: false, new_record: true, allow_destroy: true do |r| %>
<% if r.object.attribute0 %>
<%= render partial: "admin/table1/show/form_table2", locals: { r: r } %>
<% end %>
<% end %>
code1 的输出是:
code2 的输出是:
我不知道如何删除第二个只有删除的框...我只想要第一个有 ID 2 和删除勾号的框。
谢谢
创建排除不需要的记录的关系。
class Table1
has_many :table2s
has_many :active_table2s, -> {where(attribute0: true)}, class_name: 'Table2'
accepts_nested_attributes_for :active_table2s ...
然后使用该关系而不是全包关系
我有一个 Table1 与 Table2.
有 has_many 关系代码 1:-
## _edit_table1.html.erb
<% f.has_many :table2_relations, heading: false, new_record: true, allow_destroy: true do |r| %>
<%= render partial: "admin/table1/show/form_table2", locals: { r: r } %>
<% end %>
我想根据 Table2 的属性 0 显示部分,这是一个布尔值。仅当 attribute0 为真时,我才想显示部分。 我试过了:
代码 2:-
## _edit_table1.html.erb
<% f.has_many :table2_relations, heading: false, new_record: true, allow_destroy: true do |r| %>
<% if r.object.attribute0 %>
<%= render partial: "admin/table1/show/form_table2", locals: { r: r } %>
<% end %>
<% end %>
code1 的输出是:
code2 的输出是:
我不知道如何删除第二个只有删除的框...我只想要第一个有 ID 2 和删除勾号的框。 谢谢
创建排除不需要的记录的关系。
class Table1
has_many :table2s
has_many :active_table2s, -> {where(attribute0: true)}, class_name: 'Table2'
accepts_nested_attributes_for :active_table2s ...
然后使用该关系而不是全包关系