42

我有一个invoices_controller资源路线。如下:

resources :invoices do
  resources :items, only: [:create, :destroy, :update]
end

现在我想向发票添加发送功能,如何添加自定义路由invoices/:id/send来发送请求,invoices#send_invoice以及我应该如何在视图中链接到它。

传统的rails方法是什么。谢谢。

4

5 回答 5

47

在您的路线中添加:

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 方法的束缚

于 2013-05-22T13:43:25.130 回答
4
resources :invoices do
  resources :items, only: [:create, :destroy, :update]
  get 'send', on: :member
end

<%= link_to 'Send', send_invoice_path(@invoice) %>

它将转到send您的invoices_controller.

于 2013-05-22T13:40:50.873 回答
1
match '/invoices/:id/send' => 'invoices#send_invoice', :as => :some_name

添加链接

<%= button_to "Send Invoice", some_name_path(@invoice) %>
于 2013-05-22T13:48:55.667 回答
1

在 Rails >= 4 中,您可以通过以下方式完成:

match 'gallery_:id' => 'gallery#show', :via => [:get], :as => 'gallery_show'

于 2017-02-05T13:28:52.027 回答
0

资源:发票确实得到“发送”,在::会员端

于 2020-06-03T23:21:23.030 回答