Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 e.g. /handicap/tees/index responds. However, when I go to /rails/routes it only shows:

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. How do I list the engine routes in the browser?

like image 285
Obromios Avatar asked Jan 23 '17 09:01

Obromios


People also ask

How do I see routes in Rails?

Decoding the http request TIP: If you ever want to list all the routes of your application you can use rails routes on your terminal and if you want to list routes of a specific resource, you can use rails routes | grep hotel . This will list all the routes of Hotel.

How do routes work in Rails?

Rails routing is a two-way piece of machinery – rather as if you could turn trees into paper, and then turn paper back into trees. Specifically, it both connects incoming HTTP requests to the code in your application's controllers, and helps you generate URLs without having to hard-code them as strings.

How many types of routes are there in Rails?

Rails RESTful Design which creates seven routes all mapping to the user controller. Rails also allows you to define multiple resources in one line.

What is a Rails engine?

1 What are Engines? Engines can be considered miniature applications that provide functionality to their host applications. A Rails application is actually just a "supercharged" engine, with the Rails::Application class inheriting a lot of its behavior from Rails::Engine .


1 Answers

If you only want to show your routes in the browser in development mode, there's a rails page which you can call:

http://localhost:3000/rails/info/routes (available since 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.


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:)

Attempt 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:

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:

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>
like image 145
siegy22 Avatar answered Oct 14 '22 17:10

siegy22