URL Routing Application In a Ruby on Rails application I want to have this routing:


for URL www.example.com/reports go to controller documents and action reports
for URL www.example.com/charts go to controller documents and action charts


In the routes.rb file I currently have:

scope controller: :documents do
  get 'reports' =&gt; :reports
  get 'charts'  =&gt; :charts
end


The code works as expected, but is there a way so I keep the code dry and don't repeat the action names?
You could do the following. (I assume you already have a resourceful route for your documents right?)

resources :documents do
  collection do 
    get 'reports'
    get 'charts'
  end
end


To take advantage of the nested routing provided by rails (see the docs)

Which will generate the following urls:

           Prefix Verb   URI Pattern                                                                    Controller#Action
reports_documents GET    /documents/reports(.:format)                                                   documents#reports
 charts_documents GET    /documents/charts(.:format)                                                    documents#charts


If you don't have any resourceful route, you have to declare it like you already did. The only thing to do would be iterating over an array like this:

scope controller: :documents do
  %w(reports charts).each do |action|
    get action
  end
end


But IMO this hurts the readability of the code, and is only necessary when dealing with a lot of routes.