Rails:从URL解析路由信息

时间:2010-04-28 22:59:26

标签: ruby-on-rails url routing

如何解析url字符串以像

一样散列
{:controller => 'controller_name', :action => 'action_name', :id => 'id'}

2 个答案:

答案 0 :(得分:31)

在Rails 3中,您可以执行以下操作:

Rails.application.routes.recognize_path "/accounts/1"
# {:action=>"show", :controller=>"accounts", :id=>"1"}

使用ActionController :: Routing :: Routes.recognize_path不断抛出ActionController :: RoutingError例外:没有路由匹配“/ accounts / 1

答案 1 :(得分:11)

您可以使用ActionController::Routing::Routes.recognize_path,具体取决于网址的格式:

>> ActionController::Routing::Routes.recognize_path("/accounts/1",:method=>:get)`
# {:action=>"show":controller=>"accounts",:id=>"1"}