Active Admin表单:has_many按条件在单独的面板中输入

时间:2018-06-26 04:59:28

标签: html ruby-on-rails-5 activeadmin has-many-through formtastic

我正在尝试使用活动的admin和formtastic来创建一个表单,在该表单中,可以在单独的面板上编辑特定类型的评论,目前,我通过.html.erb部分渲染面板,如下所示:

<div class="panel">
  <h3>Admin Reviews</h3>
  <div class="panel_contents">
    <div>
      <% f.has_many :reviews, heading: false, new_record: true, allow_destroy: false do |review| %>
        <% next unless review.object.reviewer_type === "Admin" %>
        <%= render partial: "admin/admin_reviews", locals: { r: review } %>
        <% review.input :_destroy, :as => :boolean, :label => "Delete" %>
      <% end %>
    </div>
  </div>
</div>

<div class="panel">
  <h3>Reviews</h3>
  <div class="panel_contents">
  <div>
    <% f.has_many :reviews, heading: false, new_record: true, allow_destroy: false do |review| %>
       <% next if review.object.reviewer_type === "Admin" %>
       <%= render partial: "admin/form_reviews", locals: { r: review } %>
       <% review.input :_destroy, :as => :boolean, :label => "Delete" %>
    <% end %>
  </div>
</div>

但是我宁愿做一些事情来摆脱空白的div,该空白的div是每个迭代的评论的迭代结果,该评论属于另一个面板(blank div):

<div class="panel">
  <h3>Admin Reviews</h3>
  <div class="panel_contents">
    <div>
      <% admin_reviews = Review.where(reviewer_type: "Admin") %>
      <% f.has_many admin_reviews, heading: false, new_record: true, allow_destroy: true do |review| %>
        <%= render partial: "admin/admin_reviews", locals: { r: review } %>
      <% end %>
    </div>
  </div>
</div>

<div class="panel">
  <h3>Reviews</h3>
  <div class="panel_contents">
  <div>
    <% reviews = Review.where.not(reviewer_type: "Admin") %>
    <% f.has_many reviews, heading: false, new_record: true, allow_destroy: true do |review| %>
       <%= render partial: "admin/form_reviews", locals: { r: review } %>
    <% end %>
  </div>
</div>

上面的方法不起作用,但是我希望它能传达出我对一个整洁的解决方案的需求,该解决方案没有我正在考虑使用javascript隐藏的空白div。

1 个答案:

答案 0 :(得分:0)

<div class="panel">
  <h3>Reviews</h3>
    <div class="panel_contents">
      <div>
        <% f.has_many :reviews, for: [:reviews, f.object.non_admin_reviews], heading: false, new_record: true, allow_destroy: true do |review| %>
          <%= render partial: "admin/tutors/form_tutor_reviews", locals: { r: review } %>
        <% end %>
     </div>
   </div>
</div>