在rails中解析的路径
我正在寻找解析路径路径的方法,如下所示:
ActionController::Routing.new("post_path").parse #=> {:controller => "posts", :action => "index"}
它应该与url_for
相反
UPD
我发现: Rails中url_for的反义词是什么? 获取路径并生成解释路线的函数?
ActionController::Routing::Routes.recognize_path("/posts")
所以现在我需要将posts_path
转换为“/ posts”
有这种方法:
>> ActionController::Routing::Routes.recognize_path("/posts/") => {:action=>"index", :controller=>"posts"}
如果你只有一个包含你的路线的字符串(比如"posts_path"
),那么我想在你正在使用它的上下文中你应该可以做到
ActionController::Routing::Routes.recognize_path(send("posts_path".to_sym))
顺便说一句,这也是为我教育:)
在Rails 3中,您可以执行以下操作:
Rails.application.routes.recognize_path "/accounts/1" # {:action=>"show", :controller=>"accounts", :id=>"1"}
使用ActionController::Routing::Routes.recognize_path
继续抛出
ActionController :: RoutingErrorexception:没有路由匹配“/ accounts / 1