在Rails控制台会话中识别路线 [英] Recognize routes in rails console Session

查看:68
本文介绍了在Rails控制台会话中识别路线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有一个路由器助手,我想了解更多信息,例如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的博客中的示例进行了摘要,展示了如何手动检查URL -到控制器/动作映射和相反。例如,以

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆