Handy introspection for debugging Rails routes

I always forget how to do this, so leave this here partly as a note to myself. From Zobie’s Blog and Mike Blyth’s Stack Overflow answer

 

 routes = Rails.application.routes
 
 # figure out what route a path maps to:
 routes.recognize_path "/station/index/42.html"
 #  => {:controller=>"station", :action=>"index", :format=>"html", :id=>"42"}
 # or get a ActionController::RoutingError

 # figure out what url is generated for params, what url corresponds
 # to certain controller/action/parameters...
 r.generate :controller => :station, :action=> :index, :id=>42

If you have an isolated Rails engine mounted, it’s paths seem to not be accessible from the
`Rails.application.routes` router. You may need to try that specific engine’s router, like `Spree::Core::Engine.routes`.

It seems to me there’s got to be a way to get the actual ‘master’ router that’s actually used
for recognizing incoming urls, since there’s got to be one that sends to the mounted engine
routes as appropriate based on paths. But I haven’t figured out how to do that.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s