用于销毁的语义嵌套形式

Semantic Nested Form for Destroy

我有这个语义嵌套形式来销毁 Active Admin 中的嵌套对象。

= semantic_form_for book.chapters.new, url: {controller: "admin/products", action: :remove_chapter} do |f|
  = f.inputs do
    li
      label Select
      = f.collection_select :chapter_product_id, book.chapter_products.order(:name), :id, :name, include_blank: 'Select Chapter'

  = f.actions do
    = f.action :submit, label: "Remove Chapter"

它工作正常,但我觉得使用 semantic_for_for book.chapters.new 是错误的,因为我实际上并没有创建任何东西。

我只是制作一个基于集合的下拉列表 select 以删除一个对象。

只是想看看是否有比我现在做的更好的方法。

我想在 table_for 中添加一个删除按钮,但我似乎无法在不出现路由错误的情况下正确定向到 member_action,即使我使用的是完全相同的路由作为 semantic_form 中的控制器(并添加了 method: :post)。

尝试了此线程中的一大堆选项但没有成功 -- https://github.com/activeadmin/activeadmin/issues/53

谢谢!

我只是从另一个更简单的角度想到了它。

我直接链接到嵌套资源的销毁,而不是尝试在母对象中创建 member_action。

table_for product.chapters do
  ...
  column(:delete) { |c| link_to 'Delete', admin_chapter_path(c), class: 'member_link', method: :delete, 'data-confirm': "Are you sure you want to delete this?" }
end

因为我想被重定向回我来自的同一页面,所以我只是侵入了嵌套对象页面上的控制器方法并从那里管理我的重定向。

ActiveAdmin.register Chapter do
  actions :index, :show, :destroy

  controller do
    def destroy
      ...
      redirect_back fallback_location: admin_chapter_path, notice: "Message"
    end
  end
end