How Get All Routes in My Rails Application

Rails 3: I want to list all paths defined in my rails application

rake routes

or

bundle exec rake routes

how get all routes in my rails application?

You could have a look at way rails spits out those routes from the rake task. It's in /gems/rails/2.3.x/lib/tasks/routes.rake for Rails 2. Seems to be basically doing ActionController::Routing::Routes.routes in the general case and then interrogating that.

Rails.application.routes list all routes and determine controller

This should work if I understand your question correctly:

Rails.application.routes.routes.named_routes.values.map(&:defaults)

Then you can do something like this:

Rails.application.routes.routes.named_routes.values.map do |route|
"#{route.defaults[:controller]}#{route.defaults[:action]}"
end

Rails get list of routes as paths with parameters

You can do this quite simply by looking at how rails dumps the routes with the rails routes command.

# frozen_string_literal: true

require "rails/command"

module Rails
module Command
class RoutesCommand < Base # :nodoc:
class_option :controller, aliases: "-c", desc: "Filter by a specific controller, e.g. PostsController or Admin::PostsController."
class_option :grep, aliases: "-g", desc: "Grep routes by a specific pattern."
class_option :expanded, type: :boolean, aliases: "-E", desc: "Print routes expanded vertically with parts explained."

def perform(*)
require_application_and_environment!
require "action_dispatch/routing/inspector"

say inspector.format(formatter, routes_filter)
end

private
def inspector
ActionDispatch::Routing::RoutesInspector.new(Rails.application.routes.routes)
end

def formatter
if options.key?("expanded")
ActionDispatch::Routing::ConsoleFormatter::Expanded.new
else
ActionDispatch::Routing::ConsoleFormatter::Sheet.new
end
end

def routes_filter
options.symbolize_keys.slice(:controller, :grep)
end
end
end
end

The key here is:

inspector.format(formatter, routes_filter)

Where formatter is really just a class that responds to section, header and result:

# formats routes as a simple array of hashes
class HashFormatter

def initialize
@buffer = []
end

# called for the main routes and also for each
# mounted engine
def section(routes)
routes.each do |r|
@buffer << r.slice(:name, :verb, :path)
end
end

# this method does not need to do anything since the "headers" are
# part of the hashes
def header(routes)
end

def result
@buffer
end
end

We can then invoke our formatter with:

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

And get an array of hashes:

[{:name=>"", :verb=>"GET", :path=>"/pizzas/:foo(.:format)"}, {:name=>"", :verb=>"GET", :path=>"/pizzas/:foo/:bar(.:format)"}, {:name=>"", :verb=>"GET", :path=>"/pizzas/:foo/:bar/:baz(.:format)"}, {:name=>"foo", :verb=>"DELETE", :path=>"/foo(.:format)"}, {:name=>"root", :verb=>"GET", :path=>"/"}, #...]

The advantage here is that you're piggybacking on existing code that gathers the routes for any mounted engines and rejects internal routes.

Rails 3: Get list of routes in namespace programmatically

test_routes = []

Rails.application.routes.routes.each do |route|
route = route.path.spec.to_s
test_routes << route if route.starts_with?('/admin')
end

How to view rails/routes for a rails engine 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:

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>

Display list of routes/controllers in Rails application as UI

http://localhost:3000/rails/info/routes

Try with a 3000 port number - should work fine.

You can also call rails routes from your terminal.

In Rails, how to see all the path and url methods added by Rails's routing? (update: using Rails console)

Rails 3.x–6.x

Rails.application.routes.named_routes.helper_names

Rails 2.x

helpers = Rails.application.routes.named_routes.helpers

This will get you all the named route methods that were created. Then you can do helpers.map(&:to_s), and whatever regex you want to get your foo versions



Related Topics



Leave a reply



Submit