ActiveAdmin-创建新记录的自定义操作(缺少必需的键::id)

时间:2018-10-25 09:47:50

标签: ruby-on-rails activeadmin

我正在尝试创建一个自定义操作,该操作允许创建新记录(除了默认的new和create动作)。

我只是通过action_item添加了一个新的成员操作和一个链接。链接生成已失败,No route matches {:action=>"new_photo_order", :controller=>"admin/orders"} missing required keys: [:id]

action_item :new_photo_order, only: :index do
  link_to 'New photo order', new_photo_order_admin_order_path
end

member_action :new_photo_order, method: [:get, :post] do
  if request.post?
    # create resource
  else
    # render template with form
  end
end

我找不到任何解决方案,所以我问您应该如何添加允许添加新记录的自定义操作?

1 个答案:

答案 0 :(得分:1)

更改此

member_action :new_photo_order, method: [:get, :post] do
  if request.post?
    # create resource
  else
    # render template with form
  end
end

对此

collection_action :new_photo_order, method: [:get, :post] do
      if request.post?
        # create resource
      else
        # render template with form
      end
    end

因为当我们定义一个member_action时,它定义了一个类似orders/:id/new_photo_order的路由,该路由期望id。或者如果您想使用member_action,则需要像这样在action_item中传递订单ID

action_item :new_photo_order, only: :index do
  link_to 'New photo order', new_photo_order_admin_order_path(order.id)
end