59

Say I have a router helper that I want more info on, like blogs_path, how do I find out the map statements behind that in console.

I tried generate and recognize and I got unrecognized method error, even after I did require 'config/routes.rb'

0

5 Answers 5

103

There is a good summary with examples at Zobie's Blog showing how to manually check URL-to-controller/action mapping and the converse. For example, start with

 r = Rails.application.routes

to access the routes object (Zobie's page, a couple years old, says to use ActionController::Routing::Routes, but that's now deprecated in favor of Rails.application.routes). You can then check the routing based on a URL:

 >> r.recognize_path "/station/index/42.html"
 => {:controller=>"station", :action=>"index", :format=>"html", :id=>"42"}

and see what URL is generated for a given controller/action/parameters combination:

 >> r.generate :controller => :station, :action=> :index, :id=>42
 => /station/index/42

Thanks, Zobie!

4
  • 4
    Nice. Is there a way you can test named routes like root_path with this method? Nov 21, 2011 at 20:25
  • 6
    @brittohalloran Rails.application.routes.url_helpers.my_path_helper
    – Rahul garg
    Nov 27, 2012 at 17:56
  • This is awesome, this trick helped me solve another problem, thanks alot :)
    – kasperite
    Jun 10, 2013 at 3:34
  • 2
    The method recognize_path accepts a hash of options, so to check against a specific HTTP verb you can use: r.recognize_path "/station/submit", method: "POST"
    – lorefnon
    May 29, 2014 at 6:20
58

In the console of a Rails 3.2 app:

# include routing and URL helpers
include ActionDispatch::Routing
include Rails.application.routes.url_helpers

# use routes normally
users_path #=> "/users"
2
  • 3
    Doesn't seem like the include ActionDispatch::Routing is needed. Sep 9, 2012 at 3:56
  • 1
    This is a great candidate for inclusion in one's .pryrc or .irbrc file
    – user456584
    Sep 30, 2014 at 16:29
33

Basically(if I understood your question right) it boils down to including the UrlWriter Module:

   include ActionController::UrlWriter
   root_path
   => "/"

Or you can prepend app to the calls in the console e.g.:

   ruby-1.9.2-p136 :002 > app.root_path
   => "/" 

(This is all Rails v. 3.0.3)

2
  • 27
    include Rails.application.routes.url_helpers is Rails 3 way to do it.
    – Mirko
    May 30, 2011 at 22:43
  • 1
    for some routes you need to set a default host for them to work e.g. default_url_options[:host] = 'foo' May 31, 2011 at 6:23
3

running the routes command from your project directory will display your routing:

rake routes

is this what you had in mind?

1
  • rake routes will give all routes. I'm trying for something more specific like .generate and .recognize methods, except for helpers. So if I enter in blogs_path, I should get :action => index, :controller => :blog
    – sent-hil
    May 7, 2010 at 5:49
3

If you are seeing errors like

ActionController::RoutingError: No route matches

Where it should be working, you may be using a rails gem or engine that does something like Spree does where it prepends routes, you may need to do something else to view routes in console.

In spree's case, this is in the routes file

Spree::Core::Engine.routes.prepend do
  ...
end

And to work like @mike-blythe suggests, you would then do this before generate or recognize_path.

r = Spree::Core::Engine.routes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.