Reputation: 6119
I have an invoices_controller
which has resource routes. Like following:
resources :invoices do
resources :items, only: [:create, :destroy, :update]
end
Now I want to add a send functionality to the invoice, How do I add a custom route as invoices/:id/send
that dispatch the request to say invoices#send_invoice
and how should I link to it in the views.
What is the conventional rails way to do it. Thanks.
Upvotes: 48
Views: 34670
Reputation: 786
In Rails >= 4, you can accomplish that with:
match 'gallery_:id' => 'gallery#show', :via => [:get], :as => 'gallery_show'
Upvotes: 1
Reputation: 47532
match '/invoices/:id/send' => 'invoices#send_invoice', :as => :some_name
To add link
<%= button_to "Send Invoice", some_name_path(@invoice) %>
Upvotes: 1
Reputation: 27483
Add this in your routes:
resources :invoices do
post :send, on: :member
end
Or
resources :invoices do
member do
post :send
end
end
Then in your views:
<%= button_to "Send Invoice", send_invoice_path(@invoice) %>
Or
<%= link_to "Send Invoice", send_invoice_path(@invoice), method: :post %>
Of course, you are not tied to the POST method
Upvotes: 53
Reputation: 6274
resources :invoices do
resources :items, only: [:create, :destroy, :update]
get 'send', on: :member
end
<%= link_to 'Send', send_invoice_path(@invoice) %>
It will go to the send
action of your invoices_controller
.
Upvotes: 4