我正在尝试创建一个自定义操作,该操作允许创建新记录(除了默认的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
我找不到任何解决方案,所以我问您应该如何添加允许添加新记录的自定义操作?
答案 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