编程技术网

关注微信公众号,定时推送前沿、专业、深度的编程技术资料。

 找回密码
 立即注册

QQ登录

只需一步,快速开始

极客时间

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

Zeeyed 人工智能 2022-5-7 09:43 8人围观

腾讯云服务器
在 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", : 

并查看为给定的控制器/动作/参数组合生成的 URL:

and see what URL is generated for a given controller/action/parameters combination:

 >> r.generate :controller => :station, :action=> :index, : => /station/index/42 

谢谢,佐比!

这篇关于在 Rails 控制台会话中识别路线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程技术网(www.editcode.net)!

腾讯云服务器

相关推荐

阿里云服务器
关注微信
^