Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails: Parse route information from URL

How can I parse url string to hash like

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

?

like image 503
Andrey Kuznetsov Avatar asked Apr 28 '10 22:04

Andrey Kuznetsov


2 Answers

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

like image 69
Matt Simpson Avatar answered Nov 19 '22 14:11

Matt Simpson


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"}
like image 37
zetetic Avatar answered Nov 19 '22 14:11

zetetic