假设我有一个路由器助手,我想了解更多信息,比如blogs_path,我如何在控制台中找到它背后的map语句。
我尝试生成和识别,但我得到了无法识别的方法错误,即使我确实需要'config/routes.rb‘
发布于 2010-12-23 04:17:20
在Zobie's Blog上有一个很好的总结和示例,展示了如何手动检查URL到控制器/操作的映射,反之亦然。例如,开头为
r = Rails.application.routes
来访问routes对象(Zobie的页面已经有几年历史了,它建议使用ActionController::Routing::Routes
,但现在为了支持Rails.application.routes
,它被弃用了)。然后,您可以基于URL检查路由:
>> r.recognize_path "/station/index/42.html"
=> {:controller=>"station", :action=>"index", :format=>"html", :id=>"42"}
并查看为给定的控制器/操作/参数组合生成的URL:
>> r.generate :controller => :station, :action=> :index, :id=>42
=> /station/index/42
谢谢,佐比!
发布于 2012-07-13 11:25:25
在Rails 3.2应用程序的控制台中:
# include routing and URL helpers
include ActionDispatch::Routing
include Rails.application.routes.url_helpers
# use routes normally
users_path #=> "/users"
发布于 2011-02-02 23:17:59
基本上(如果我没理解错你的问题)它可以归结为包含UrlWriter模块:
include ActionController::UrlWriter
root_path
=> "/"
或者,您可以在控制台中为呼叫添加app,例如:
ruby-1.9.2-p136 :002 > app.root_path
=> "/"
(这是all Rails v.3.0.3)
https://stackoverflow.com/questions/2778074
复制相似问题