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:
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 userails routes -c account_groups
Related Topics
In Ruby How to Use Class Level Local Variable? (A Ruby Newbie's Question)
Tcp Socket Communication Between Processes on Heroku Worker Dyno
What Is the Opposite of Ruby's Include
Travis Ci: Fatal: Role Does Not Exist
How to Check Ruby Syntax Error in Ruby Code
Unpermitted Parameters for Dynamic Forms in Rails 4
Simplest Way to Send Raw Byte-Arrays Using Ruby's Tcpsocket-Class
How to Convert a Ruby String with Brackets to an Array
Ruby on Rails 4 Select Multiple
Authlogic and Multiple Sessions for the Same User
Ruby: Method Inexplicably Overwritten and Set to Nil
How to Execute Windows Cli Commands in Ruby
How to Transpose Different Sized Ruby Arrays
How to Stop Rails' Built-In Server from Listening on 0.0.0.0 by Default
Why am I Getting This Passenger Error Could Not Find Rake-0.9.2.2 in Any of the Sources