假设我有一个路由器助手,我想了解更多信息,例如 blogs_path,我如何在控制台中找到其背后的 map 语句。
我尝试生成和识别,但出现无法识别的方法错误,即使在我确实需要“config/routes.rb”之后也是如此
最佳答案
Zobie's Blog 有一个很好的总结和示例。展示如何手动检查 URL 到 Controller /操作的映射以及反之亦然。例如,以
开头 r = Rails.application.routes
访问路由对象(Zobie 的页面已有几年历史,说使用 ActionController::Routing::Routes
,但现在已弃用,取而代之的是 Rails.application.routes
)。然后您可以根据 URL 检查路由:
>> r.recognize_path "/station/index/42.html"
=> {:controller=>"station", :action=>"index", :format=>"html", :id=>"42"}
并查看为给定的 Controller /操作/参数组合生成的 URL:
>> r.generate :controller => :station, :action=> :index, :id=>42
=> /station/index/42
谢谢,佐比!
关于ruby-on-rails - 在 Rails 控制台 session 中识别路线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2778074/