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?
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.
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.
Rails RESTful Design which creates seven routes all mapping to the user controller. Rails also allows you to define multiple resources in one line.
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 .
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:)
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))
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)
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With