Elegant rails: multiple routes, one controller action

What is the most elegant way to have multiple routes for the same controller action?

I have:

get 'dashboard', to: 'dashboard#index' get 'dashboard/pending', to: 'dashboard#index' get 'dashboard/live', to: 'dashboard#index' get 'dashboard/sold', to: 'dashboard#index' 

This is pretty ugly. Any more elegant recommendations?
Bonus points for one liner.

+5
source share
1 answer

Why don't you have only one route and one controller action and differ in functionality based on the parameters passed to it?

config /routes.rb:

 get 'dashboard', to: 'dashboard#index' 

application / controller / dashboard_controller.rb

 def index ... if params[:pending] # pending related stuff end if params[:live] # live related stuff end if params[:sold] # sold related stuff end ... end 

links in views

  • Waiting: <%= link_to "Pending", dashboard_path(pending: true) %>
  • live: <%= link_to "Live", dashboard_path(live: true) %>
  • sold: <%= link_to "Sold", dashboard_path(sold: true) %>
+10
source

Source: https://habr.com/ru/post/1215785/


All Articles