Rails中url_for的反义词是什么?是否具有路径并生成解释路径的函数? [英] What is the opposite of url_for in Rails? A function that takes a path and generates the interpreted route?

查看:80
本文介绍了Rails中url_for的反义词是什么?是否具有路径并生成解释路径的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

脑袋有点炸....我如何从相对路径中获取:controller和:action的哈希值?

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

这基本上与url_for相反。在下面的示例中, some_function是我正在寻找的神秘函数名称...我知道这很简单,只是不记得了或者似乎能够在文档中找到它。

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.

就像这样:

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


推荐答案

Rspec具有方法'params_for' ,它使用Action Controller的路由方法将带有方法的路径解析为路由。

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

这篇关于Rails中url_for的反义词是什么?是否具有路径并生成解释路径的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆