1

In my applications routes.rb I have defined three routes like the following

 map.signup '/signup', :controller => 'users', :action => 'new'
 map.login  '/login', :controller => 'sessions', :action => 'new'
 map.logout '/logout', :controller => 'sessions', :action => 'destroy'

Is it possible for me to get the controller and action name for a particular path?

I am looking for some method like this...

def current_routes(a)
end

should return :controller => 'users', :action => 'new' if I call current_routes('signup_path')

ssri
  • 1,290
  • 4
  • 15
  • 23

1 Answers1

3

Try like this

ActionController::Routing::Routes.recognize_path("/posts/")

If you only have a string with your route (like "signup_path"), then I guess in the context you're using this you should be able to do

ActionController::Routing::Routes.recognize_path(send("signup_path".to_sym))

Ashish
  • 5,723
  • 2
  • 24
  • 25
  • When i use ActionController::Routing::Routes.recognize_path(send("signup_path".to_sym)) it gives me undefined method `signup_path' error. But i have defined that in my routes and if i try .to_s for the one of route in console i get ActionController::Routing::Routes.routes[1].to_s => "ANY /signup/ {:action=>\"new\", :controller=>\"users\"}" – ssri Mar 08 '11 at 11:10
  • I guess from console you cant get access of helper methods like signup_path. Try it from UI. – Ashish Mar 09 '11 at 04:50
  • @ssri The same approach does not work [here](http://stackoverflow.com/q/42691729/1610034), any thoughts? – Saurabh Mar 14 '17 at 08:01