Rails: 从URL中解析路由信息

17

如何将URL字符串解析为哈希值,例如

{:controller => 'controller_name', :action => 'action_name', :id => 'id'}

你的问题没有意义。它取决于URL的格式。 - Gabriel Ščerbák
1
我有同样的问题...无论url格式如何。即,url_for的反函数是什么? - Corin
2个回答

36

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

Rails.application.routes.recognize_path "/accounts/1"
# {:action=>"show", :controller=>"accounts", :id=>"1"}
使用ActionController::Routing::Routes.recognize_path时,始终抛出ActionController::RoutingError异常:没有匹配"/accounts/1"的路由。

12

根据URL的格式,您可以尝试使用ActionController::Routing::Routes.recognize_path

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

不幸的是,这会丢弃额外的参数,例如“/accounts/1?verify=true”,它会丢弃 verify。 - user1130176

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接