Rails 2.3.5: how to get routes name

2019-08-04 03:53发布

问题:

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')

回答1:

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))