Ruby-on-rails 识别 rails 控制台会话中的路由
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2778074/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Recognize routes in rails console Session
提问by sent-hil
Say I have a router helper that I want more info on, like blogs_path, how do I find out the map statements behind that in console.
假设我有一个我想要更多信息的路由器助手,比如 blogs_path,我如何在控制台中找出它背后的 map 语句。
I tried generate and recognize and I got unrecognized method error, even after I did require 'config/routes.rb'
我尝试生成和识别,但我得到了无法识别的方法错误,即使在我确实需要 'config/routes.rb' 之后
回答by Mike Blyth
There is a good summary with examples at Zobie's Blogshowing how to manually check URL-to-controller/action mapping and the converse. For example, start with
Zobie 的博客中有一个很好的总结和示例,展示了如何手动检查 URL 到控制器/操作的映射以及相反的操作。例如,从
r = Rails.application.routes
to access the routes object (Zobie's page, a couple years old, says to use ActionController::Routing::Routes, but that's now deprecated in favor of Rails.application.routes). You can then check the routing based on a URL:
访问路由对象(Zobie 的页面,几年前,说要使用ActionController::Routing::Routes,但现在不赞成使用Rails.application.routes)。然后,您可以根据 URL 检查路由:
>> r.recognize_path "/station/index/42.html"
=> {:controller=>"station", :action=>"index", :format=>"html", :id=>"42"}
and see what URL is generated for a given controller/action/parameters combination:
并查看为给定控制器/操作/参数组合生成的 URL:
>> r.generate :controller => :station, :action=> :index, :id=>42
=> /station/index/42
Thanks, Zobie!
谢谢,佐比!
回答by Nick
In the console of a Rails 3.2 app:
在 Rails 3.2 应用程序的控制台中:
# include routing and URL helpers
include ActionDispatch::Routing
include Rails.application.routes.url_helpers
# use routes normally
users_path #=> "/users"
回答by Sam Figueroa
Basically(if I understood your question right) it boils down to including the UrlWriter Module:
基本上(如果我理解你的问题)归结为包括 UrlWriter 模块:
include ActionController::UrlWriter
root_path
=> "/"
Or you can prepend app to the calls in the console e.g.:
或者,您可以将应用程序添加到控制台中的调用中,例如:
ruby-1.9.2-p136 :002 > app.root_path
=> "/"
(This is all Rails v. 3.0.3)
(这都是 Rails v. 3.0.3)
回答by Mark Swardstrom
If you are seeing errors like
如果您看到类似的错误
ActionController::RoutingError: No route matches
Where it should be working, you may be using a rails gem or engine that does something like Spree does where it prepends routes, you may need to do something else to view routes in console.
在它应该工作的地方,您可能正在使用 Rails gem 或引擎,它可以像 Spree 那样在路由之前执行某些操作,您可能需要执行其他操作才能在控制台中查看路由。
In spree's case, this is in the routes file
在狂欢的情况下,这是在路由文件中
Spree::Core::Engine.routes.prepend do
...
end
And to work like @mike-blythe suggests, you would then do this before generateor recognize_path.
要像@mike-blythe 建议的那样工作,您可以在generate或之前执行此操作recognize_path。
r = Spree::Core::Engine.routes
回答by Pete
running the routes command from your project directory will display your routing:
从您的项目目录运行 routes 命令将显示您的路由:
rake routes
is this what you had in mind?
这是你的想法吗?

