Skip to content

Instantly share code, notes, and snippets.

@lenage
Forked from toamitkumar/gist:952211
Created February 27, 2012 15:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lenage/1924529 to your computer and use it in GitHub Desktop.
Save lenage/1924529 to your computer and use it in GitHub Desktop.
Inspect and test routes on console (Rails 3)
$ rails console
Loading development environment (Rails 3.0.6)
ruby-1.8.7-p334 :001 > r = Rails.application.routes
Gives you a handle of all the routes (config/routes.rb)
#Inspect a named route:
ruby-1.8.7-p334 :005 > r.recognize_path(app.destroy_user_session_path)
=> {:action=>"destroy", :controller=>"sessions"}
#Check all the named routes:
ruby-1.8.7-p334 :013 > r.routes.each { |x| puts x.path }.compact
/not_authorized(.:format)
/admin_not_authorized(.:format)
/financial(.:format)
/care(.:format)
/first_login(.:format)
/first_login/:id(.:format)
/users/sign_out(.:format)
/users/sign_in(.:format)
/users/sign_in(.:format)
......
#Inspect a particular route:
ruby-1.8.7-p334 :017 > my_route_paths = r.routes.map{|c| c.path if(c.path.include?("users"))}.compact
=> ["/users/sign_out(.:format)", "/users/sign_in(.:format)", "/users/sign_in(.:format)", "/users/sign_out(.:format)", "/users/password(.:format)", "/users/password/new(.:format)", "/users/password/edit(.:format)", "/users/password(.:format)", "/users/:id/current_client(.:format)", "/users/:id/set_client(.:format)", "/users(.:format)", "/users(.:format)", "/users/new(.:format)", "/users/:id/edit(.:format)", "/users/:id(.:format)", "/users/:id(.:format)", "/users/:id(.:format)"]
ruby-1.8.7-p334 :022 > my_routes = r.routes.map{|c| c if(c.path.include?("users"))}.compact
ruby-1.8.7-p334 :024 > my_routes.first.path
=> "/users/sign_out(.:format)"
ruby-1.8.7-p334 :026 > my_routes.first.requirements
=> {:action=>"destroy", :controller=>"sessions"}
ruby-1.8.7-p334 :028 > my_routes.first.verb
=> "GET"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment