Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recognize routes in rails console Session

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!


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"

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)


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

rake routes

is this what you had in mind?