我有一个 invoices_controller
其中有资源路线。如下:
resources :invoices do
resources :items, only: [:create, :destroy, :update]
end
现在我想在发票中添加发送功能,如何将自定义路由添加为
invoices/:id/send
发送请求说 invoices#send_invoice
以及我应该如何在 View 中链接到它。传统的rails方法是什么。谢谢。
最佳答案
在您的 route 添加:
resources :invoices do
post :send, on: :member
end
或者
resources :invoices do
member do
post :send
end
end
那么在你看来:
<%= button_to "Send Invoice", send_invoice_path(@invoice) %>
或者
<%= link_to "Send Invoice", send_invoice_path(@invoice), method: :post %>
当然,您不受 POST 方法的束缚
关于ruby-on-rails - 如何将自定义路由添加到资源路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16693185/