Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@presidentbeef
Last active September 1, 2020 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save presidentbeef/9c0b32b3d7f46e2cd970ca0ae8972c9b to your computer and use it in GitHub Desktop.
Save presidentbeef/9c0b32b3d7f46e2cd970ca0ae8972c9b to your computer and use it in GitHub Desktop.
Access Rails routes programmatically

RouteReader

# Save this file as `rr.rb`
#
# Run `./bin/rails c` to start Rails console.
#
# Then in Rails console run:
# require './rr'; routes = RouteReader.routes
#
# `routes` will be an Array of Hashes. Each entry is a route.
class RouteReader
attr_reader :routes
def self.routes
rr = self.new
ActionDispatch::Routing::RoutesInspector.new(Rails.application.routes.routes).format(rr)
rr.routes
end
def initialize
@routes = []
end
def section(routes)
@routes.concat(routes)
end
def result; end
def section_title(title); end
def header(routes); end
def no_routes(routes); end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment