Andrey Kuznetsov
Andrey Kuznetsov

Reputation: 11840

Rails: Parse route information from URL

How can I parse url string to hash like

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

?

Upvotes: 17

Views: 7009

Answers (2)

zetetic
zetetic

Reputation: 47578

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"}

Upvotes: 12

Matt Simpson
Matt Simpson

Reputation: 502

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

Upvotes: 37

Related Questions