ActiveAdmin has_many有条件地部分显示

问题描述 投票:0回答:1

我有一个Table1与Table2具有has_many关系。在_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的attribute0显示局部变量,它是一个布尔值。仅当attribute0为true时,我才要显示部分内容。我试过了:

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

代码1的输出是:

enter image description here

代码2的输出是:

enter image description here

我不知道如何删除仅具有Delete的第二个框...我只希望具有ID 2的第一个框并删除刻度。谢谢

ruby-on-rails activeadmin
1个回答
0
投票

创建一个不包含不需要的记录的关系。

class Table1
  has_many :table2s
  has_many :active_table2s, -> {where(attribute0: true)}, class_name: 'Table2'
  accepts_nested_attributes_for :active_table2s ...

然后使用该关系而不是全部包含的关系

© www.soinside.com 2019 - 2024. All rights reserved.