{:controller => "posts", :act-6ren">
gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 07:09:42 27 4
gpt4 key购买 nike

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

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com