Rails: Parse route information from URL

断了今生、忘了曾经 提交于 2019-11-27 21:42:24

问题


How can I parse url string to hash like

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

?


回答1:


In Rails 3 you can do the following:

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

Using ActionController::Routing::Routes.recognize_path kept throwing ActionController::RoutingError Exception: No route matches "/accounts/1




回答2:


You may be able to use ActionController::Routing::Routes.recognize_path, depending on the format of the URL:

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


来源:https://stackoverflow.com/questions/2733678/rails-parse-route-information-from-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!