ruby-on-rails - rails 中的路径解析

标签 ruby-on-rails ruby parsing routing

我正在寻找这样解析路由路径的方法:

ActionController::Routing.new("post_path").parse
#=> {:controller => "posts", :action => "index"}

应该和url_for相反

更新
我发现: What is the opposite of url_for in Rails? A function that takes a path and generates the interpreted route?

ActionController::Routing::Routes.recognize_path("/posts")

所以现在我需要将 posts_path 转换为“/posts”

最佳答案

在 Rails 3 中,您可以执行以下操作:

Rails.application.routes.recognize_path "/accounts/1"
# {:action=>"show", :controller=>"accounts", :id=>"1"}

使用 ActionController::Routing::Routes.recognize_path 不断抛出

ActionController::RoutingError Exception: No route matches "/accounts/1

关于ruby-on-rails - rails 中的路径解析,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2714867/

相关文章:

ruby-on-rails - Rails 根据表单输入重新加载 chartkick

mysql - Rails - 如何遍历行值为 'x' 的数据库表列并显示在 html 表中

ruby - 为什么语法突出显示不适用于 Chrome Devtools 中的 Ruby 文件 (.erb)?

ruby - 尝试循环遍历 Vagrant 框的哈希,失败

PHP 解析错误 : syntax error, 意外的 T_STRING,期待 T_FUNCTION

ruby-on-rails - 使用 Controller 规范的单行语法

mysql - Rails/Devise/ActiveRecord 不接受 unicode 字符

mysql - ActiveRecord index_by 重复键的优先级

C# 解析 SqlDbType 转换

c# - 遍历具有复杂条件表达式的 ast 以生成 linq 表达式