Result of 'Rake Routes' in Ruby Script

Result of `rake routes` in ruby script

Rails.application.routes.routes.to_a

.to_a is optional, it just converts it to an array.

(I found this line in railties/lib/rails/tasks/routes.rake)

I use it like :
routes[10].defaults => {:action=>"edit", :controller=>"polls"}

Edit : You can find the (quite hacky) way I do my routing specs here : https://gist.github.com/52ac6d848ce0d9fd52ac

How to edit output of rake routes

Have you tried accessing the HTML Routes view?

You can enable it adding this route get '/rails/info/routes' => 'routes' in your config/routes.rb file and then access the http://localhost:3000/rails/info/routes and you will see something like this:

Sample Image

Understanding the Rake Routes output

The first column is the name of the route, which is useful for things like setting up links (where you could use, for example population_management_path(id) instead of having to hardcode /population_management/id.

If you want to name your own custom routes, you can do that using :as, eg.

match 'patients/:id/summary', to: 'patients#show', via: :get, as: "patient_summary"
# or slightly simpler
get 'patients/:id/summary', to: 'patients#show', as: "patient_summary"

(You will need to use the latter in Rails 4, given match is being deprecated).

Either method is fine, although resources (and resource) are much more convenient when you're setting up standard RESTful resources and don't want to have to define all the routes individually.

As always, the Rails Routing guide is good reading on the subject too, and worth checking out.

How to restrict rake routes output to URI Pattern and Controller#action columns?

Given your latest requirements and the answer you posted, this may be what you want:

$ cat tst.awk
NR==1 { print $3, $4 "\t" $5; next }
{ sub("[^/]+",""); sub(/ +/,"\t"); print }

$ awk -f tst.awk file
URI Pattern Controller#Action
/users/sign_in(.:format) devise/sessions#new
/users/password(.:format) devise/passwords#update
/api/auth/sign_in(.:format) devise_token_auth/sessions#new {:format=>:json}

$ awk -f tst.awk file | column -s $'\t' -t
URI Pattern Controller#Action
/users/sign_in(.:format) devise/sessions#new
/users/password(.:format) devise/passwords#update
/api/auth/sign_in(.:format) devise_token_auth/sessions#new {:format=>:json}

Understanding rake routes output

I've always thought:

  • Line 2: Route can be accessed using profil_path with PUT method.
  • Line 4: Route can be accessed using login_path with POST method.

Conclusion: Lines with the first column empty (line 2 and 4) would
follow the one above it.

Everything's correct except the conclusion. profil_path expands to /profil/:id(.:format). If it is called with method GET it responds to your first route, if its called with method PUT it responds to your second route.

But same doesn't hold true for second set of routes. You don't have any named helper for /admin/pengguna/index/:page(.:format). If you want a named helper, you should define the route like:

get 'index/:page', :action => :index, :as => :what_ever_named_helper_you_want

Can I rake the routes for a specific resource?

You can check for routes of specific controller:

rake routes CONTROLLER=account_groups

As seen in @ZiiCEagle's answer this is deprecated now and you should use

rails routes -c account_groups



Related Topics



Leave a reply



Submit