2010-01-06 59 views

回答

2

軌的路由系統訪問以兩種方式工作,它識別並構建URL。 您需要recognize_path方法,如下面的例子所示:

ActionController::Routing::Routes.recognize_path('/mycontroller/myaction', :method => :get) 

假設URL是用something_pathsomething_url產生的,它返回:

{ :action => 'myaction', :controller => 'mycontroller' } 

從中你能提取行動部分。

+2

這並不在Rails 3中工作,順便說一句。 – coreyward 2012-07-19 21:01:57

3

你不需要猜測。您可以從終端/命令提示符運行rake routes以獲取應用程序中所有路由的列表。輸出包括使用的HTTP方法,以及調用的控制器和操作。

0

如果你在一個視圖中,可以使用

  • action_name以訪問的處理動作
  • controller_name去處理控制
0

在Rails 3:

Rails.application.routes.recognize_path("http://example.ltd/registrants") 
=> => {:action=>"index", :controller=>"registrants"} 

如果您在引擎是,活動

Events::Engine.routes.recognize_path("/registrants") 
=> {:action=>"index", :controller=>"events/registrants"} 
相關問題