Rails中的路径解析 [英] Path parsing in rails
本文介绍了Rails中的路径解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找解析这样的路由路径的方法:
I am looking for method for parsing route path like this:
ActionController::Routing.new("post_path").parse
#=> {:controller => "posts", :action => "index"}
它应该与url_for
Upd
I've found out:
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"
So now I need to convert posts_path
into "/posts"
推荐答案
有此方法:
>> ActionController::Routing::Routes.recognize_path("/posts/")
=> {:action=>"index", :controller=>"posts"}
如果您的路线只有一个字符串(例如"posts_path"
),那么我想在您正在使用的上下文中,您应该能够做到
If you only have a string with your route (like "posts_path"
), then I guess in the context you're using this you should be able to do
ActionController::Routing::Routes.recognize_path(send("posts_path".to_sym))
顺便说一句,这也对我有教育意义:)
btw this was educating for me too :)
这篇关于Rails中的路径解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文