17

How can I parse url string to hash like

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

?

2
  • Your question does not make sense. It depends on the format of the url. Commented Apr 28, 2010 at 23:13
  • 1
    I have this same question... regardless of url format. i.e. what's the inverse of url_for? Commented Jun 16, 2011 at 3:59

2 Answers 2

37

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

Sign up to request clarification or add additional context in comments.

Comments

12

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

1 Comment

unfortunately, this drops extra params, ex "/accounts/1?verify=true" it drops verify

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.