与Rails中的url_for相反的是什么? 获取路径并生成解释路线的函数?

大脑有点油炸….我如何得到:控制器的哈希和来自relative_path的动作?

这基本上与url_for相反。 在下面的例子中,“some_function”是我正在寻找的神秘函数名称……我知道它很简单,只是记不住或者似乎无法在文档中找到它。

像这样:

some_function('/posts/1/edit') => {:controller => 'posts', :action => 'edit', :id => '1'} 

Rspec有一个方法’params_for’,它使用Action Controller的Routing Methods来解析带有方法的路径到路由中。

他们比这更强大,但归结为:

 def params_for(path, method) params = ActionController::Routing::Routes.recognize_path(path, :method => method) end 

使用Rails 3最好的方法是使用

 Rails.application.routes.recognize_path('/posts/1/edit')