在 Rails 控制台会话中识别路线 [英] Recognize routes in rails console Session
问题描述
假设我有一个路由器助手,我想了解更多信息,例如 blogs_path,我如何在控制台中找到它背后的地图语句.
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.
我尝试生成和识别,但出现无法识别的方法错误,即使在我确实需要 'config/routes.rb' 之后也是如此
I tried generate and recognize and I got unrecognized method error, even after I did require 'config/routes.rb'
推荐答案
在 Zobie's Blog 展示了如何手动检查 URL-to-controller/action 映射以及反之.例如,以
There is a good summary with examples at Zobie's Blog showing how to manually check URL-to-controller/action mapping and the converse. For example, start with
r = Rails.application.routes
访问路由对象(Zobie 的页面,几年前,说使用 ActionController::Routing::Routes
,但现在不推荐使用 Rails.application.routes代码>).然后您可以根据 URL 检查路由:
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:
>> r.recognize_path "/station/index/42.html"
=> {:controller=>"station", :action=>"index", :format=>"html", :id=>"42"}
并查看为给定的控制器/动作/参数组合生成的 URL:
and see what URL is generated for a given controller/action/parameters combination:
>> r.generate :controller => :station, :action=> :index, :id=>42
=> /station/index/42
谢谢,佐比!
这篇关于在 Rails 控制台会话中识别路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!