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

查看:11
本文介绍了在 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'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屋!

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