Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the opposite of url_for in Rails? A function that takes a path and generates the interpreted route?

Brain's a little fried....How do I get a hash of the :controller and :action from a relative_path?

This is basically the opposite of url_for. in the example below, "some_function" is the mystery function name I'm looking for...I know it's easy, just can't remember or seem to be able to find it in the docs.

Like so:

some_function('/posts/1/edit')
=> {:controller => 'posts', :action => 'edit', :id => '1'}
like image 739
btelles Avatar asked Feb 08 '10 15:02

btelles


2 Answers

Rspec has a method 'params_for', which uses Action Controller's Routing Methods to parse paths with methods into routes.

Theirs is a little more robust than this, but it boils down to:

def params_for(path, method)
  params = ActionController::Routing::Routes.recognize_path(path, :method => method)
end
like image 112
Graeme Worthy Avatar answered Oct 25 '22 15:10

Graeme Worthy


With Rails 3 the best way is to use

Rails.application.routes.recognize_path('/posts/1/edit')
like image 44
Thiago Arrais Avatar answered Oct 25 '22 14:10

Thiago Arrais