2011-03-08 44 views
1

在我的應用程序的routes.rb我定義了諸如以下的Rails 2.3.5:如何獲取路線命名

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

三條路線是否有可能對我來說,爲一個特定的路徑獲得的控制器和動作名稱?

我要尋找一些這樣的方法......

def current_routes(a) 
end 

應該返回:controller => 'users', :action => 'new'如果我叫current_routes('signup_path')

+2

也許'耙路線'你在找什麼? – tjeden 2011-03-08 10:22:55

回答

3

嘗試這樣

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

如果你只有一個字符串你的路線(如「signup_path」),那麼我想你正在使用這個環境,你應該可以做

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

+0

當我使用ActionController :: Routing :: Routes.recognize_path(send(「signup_path」.to_sym)) 它給了我一個未定義的方法'signup_path'錯誤。但我已經定義,在我的路線,如果我嘗試.to_s爲控制檯中的路由我得到 ActionController :: Routing :: Routes.routes [1] .to_s =>「ANY/signup/{:action = > \「new \」,:controller => \「users \」}「 – ssri 2011-03-08 11:10:31

+0

我猜控制檯你不能訪問像signup_path這樣的輔助方法。從UI嘗試。 – Ashish 2011-03-09 04:50:40

+0

@ssri同樣的方法不起作用[here](http://stackoverflow.com/q/42691729/1610034),有什麼想法? – Saurabh 2017-03-14 08:01:49