简体   繁体   English

如何在浏览器中查看rails引擎的rails / routes

[英]How to view rails/routes for a rails engine in the browser

I have a rails engine, which is mounted in dummy/config/routes.rb using 我有一个rails引擎,它使用安装在dummy/config/routes.rb

mount Handicap::Engine => "/handicap"

In the engine, I have a number of controllers, and when I start a rails server in the dummy directory these routes are active eg /handicap/tees/index responds. 在引擎中,我有许多控制器,当我在虚拟目录中启动rails服务器时,这些路由是活动的,例如/ handicap / tees / index responds。 However, when I go to /rails/routes it only shows: 但是,当我转到/rails/routes ,它只显示:

handicap_path       /handicap   Handicap::Engine
rails_routes_path   GET /rails/routes(.:format) sextant/routes#index
sextant_engine_path     /sextant    Sextant::Engine

I can list them using rake routes , but my normal work flow is to list them in the browser. 我可以使用rake routes列出它们,但我的正常工作流程是在浏览器中列出它们。 How do I list the engine routes in the browser? 如何在浏览器中列出引擎路由?

If you only want to show your routes in the browser in development mode, there's a rails page which you can call: 如果您只想在开发模式下在浏览器中显示路由,则可以调用rails页面:

http://localhost:3000/rails/info/routes (available since Rails 4) http:// localhost:3000 / rails / info / routes (自Rails 4起可用)

If you're upgrading from rails 3, you can remove the sextant gem from your gems as this is now part of the rails core. 如果你从rails 3升级,你可以从你的宝石中删除六分仪宝石,因为它现在是rails core的一部分。


If you want to show your routes in production to the user, you can implement it like the following: (implemented in bin/rake routes ( here ) you can call the same things from your code:) 如果你想在生产中向用户展示你的路线,你可以像下面那样实现它:(在bin/rake routes这里 )实现你可以从你的代码调用相同的东西:)

Attempt 1: 尝试1:

Controller code: 控制器代码:

# app/controllers/example_controller.rb

routes = Rails.application.routes.routes
@inspector = ActionDispatch::Routing::RoutesInspector.new(routes)

View Code: 查看代码:

# app/views/example/show.html.erb

# Yeah! There's also a HTML Table Formatter already to print routes in html
inspector.format(ActionDispatch::Routing::HtmlTableFormatter.new(self))

Attempt 2: 尝试2:

Do this in a helper: 在帮手中做到这一点:

# app/helpers/route_printing_helper.rb

module RoutePrintingHelper
  def print_routes(view)
    routes = Rails.application.routes.routes
    inspector = ActionDispatch::Routing::RoutesInspector.new(routes)
    inspector.format(ActionDispatch::Routing::HtmlTableFormatter.new(view))
  end
end

And then call it: 然后叫它:

# app/views/example/show.html.erb
print_routes(self)

Attempt 3: 尝试3:

This is the "cheapest" way of doing this: 这是“最便宜”的方式:

# app/controllers/example_controller.rb

@routes_output = `#{Rails.root}/bin/rake routes`

Your view: 你的观点:

# app/views/example/show.html.erb

<pre><%= @routes_output %></pre>

I worked this out, the clue was the line: 我解决了这个问题,线索是这样的:

sextant_engine_path     /sextant    Sextant::Engine

This indicated that I still had the sextant gem in my Gemfile. 这表明我的Gemfile中仍然有六分星的宝石 Sextant was the way of doing this in rails 3, and as noted by siegy22 , it has not been needed since rails 4. The solution was simply to take sextant out of the gemfile. Sextant是在rails 3中实现这一目标的方式,正如siegy22所指出的那样 ,自导轨4以来就没有必要。解决方案只是将六分仪从gemfile中取出。 Given the project is on rails 5, it was about time! 鉴于该项目是在轨道5,这是时间了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM