Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom routes to resource route

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.

like image 610
Sathish Manohar Avatar asked May 22 '13 13:05

Sathish Manohar


People also ask

How do I add a method to resource controller?

public function __construct(){ $this->middleware('auth'); } public function index(){ $envios = Envio::all(); return view('envios.

What are resource routes?

Resource routing allows you to quickly declare all of the common routes for a given resourceful controller. A single call to resources can declare all of the necessary routes for your index , show , new , edit , create , update , and destroy actions.

What is custom routing?

Custom routing allows you to display SEO-friendly URLs on your site that map behind-the-scenes to conventional Kibo eCommerce resources such as a product page or a search results page.


1 Answers

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

like image 143
Damien Avatar answered Sep 18 '22 08:09

Damien