sen*_*hil 54 routes ruby-on-rails
假设我有一个路由器助手,我想要更多信息,比如blogs_path,我如何在控制台中查找后面的地图语句.
我尝试生成并识别并且我得到了无法识别的方法错误,即使在我确实需要'config/routes.rb'之后
Mik*_*yth 92
在Zobie的博客中有一个很好的总结示例,展示了如何手动检查URL到控制器/动作映射和相反的情况.例如,从.开始
r = Rails.application.routes
Run Code Online (Sandbox Code Playgroud)
访问路由对象(Zobie的页面,几年前,说要使用ActionController::Routing::Routes,但现在已经弃用了赞成Rails.application.routes).然后,您可以根据URL检查路由:
>> r.recognize_path "/station/index/42.html"
=> {:controller=>"station", :action=>"index", :format=>"html", :id=>"42"}
Run Code Online (Sandbox Code Playgroud)
并查看为给定的控制器/操作/参数组合生成的URL:
>> r.generate :controller => :station, :action=> :index, :id=>42
=> /station/index/42
Run Code Online (Sandbox Code Playgroud)
谢谢,Zobie!
Nic*_*ick 51
在Rails 3.2应用程序的控制台中:
# include routing and URL helpers
include ActionDispatch::Routing
include Rails.application.routes.url_helpers
# use routes normally
users_path #=> "/users"
Run Code Online (Sandbox Code Playgroud)
Sam*_*roa 34
基本上(如果我理解你的问题)它归结为包括UrlWriter模块:
include ActionController::UrlWriter
root_path
=> "/"
Run Code Online (Sandbox Code Playgroud)
或者您可以将应用程序添加到控制台中的调用,例如:
ruby-1.9.2-p136 :002 > app.root_path
=> "/"
Run Code Online (Sandbox Code Playgroud)
(这是所有Rails v.3.0.3)