Listing 'rake routes' for a mountable Rails 3.1 engine
Asked Answered
D

6

24

I'm working on a mountable engine for use with Rails 3.1, and I want to list the engine's routes.

I created the engine using:

$ rails plugin new rails_blog_engine --mountable

And edited the 'test/dummy/config/routes' file to read:

Rails.application.routes.draw do
  mount RailsBlogEngine::Engine => "/blog"
end

...and 'config/routes' to read:

RailsBlogEngine::Engine.routes.draw do
  resources :posts
end

I want to list the routes generated for ':posts', but it's not clear how I can do this. When I run 'rake app:routes', I get only the "/blog" route:

$ rake app:routes
rails_blog_engine    /blog    {:to=>RailsBlogEngine::Engine}

When I run 'rake routes', I get an error:

$ rake routes
rake aborted!
Don't know how to build task 'routes'

How can I see the routes for ':posts'? Can I do this without rewriting the relevant rake tasks?

Drogheda answered 15/9, 2011 at 13:31 Comment(2)
Now, in Rails 3.2.2, rake app:routes works fine. A simple rake routes does throw the same error, but that is expected.R
Writing an engine today, I ran into this problem, and I just noticed I had the same problem 3 years ago! (ref my comment above) Some things never change, eh? :)R
D
11

If you copy code from the standard Rails 3.1.0 rake routes task into your Rakefile, and change the top part to read:

task :routes => 'app:environment' do
  Rails.application.reload_routes!
  all_routes = RailsBlogEngine::Engine.routes.routes

...replacing RailsBlogEngine with the name of your engine, then you can get a rudimentary list of routes by running:

rake routes

Note that in Rails 3.1.1 and later, you'll need a newer version of the rake routes task.

Drogheda answered 15/9, 2011 at 14:57 Comment(1)
Thanks! Worked for me. Seems odd tho that this wouldn't be included out of the box.Braga
L
24

In case people are missing it in the comments, as of Rails 3.2.2, you can now use

$ rake app:routes
Laxity answered 3/12, 2012 at 7:22 Comment(0)
D
11

If you copy code from the standard Rails 3.1.0 rake routes task into your Rakefile, and change the top part to read:

task :routes => 'app:environment' do
  Rails.application.reload_routes!
  all_routes = RailsBlogEngine::Engine.routes.routes

...replacing RailsBlogEngine with the name of your engine, then you can get a rudimentary list of routes by running:

rake routes

Note that in Rails 3.1.1 and later, you'll need a newer version of the rake routes task.

Drogheda answered 15/9, 2011 at 14:57 Comment(1)
Thanks! Worked for me. Seems odd tho that this wouldn't be included out of the box.Braga
M
3

For rails 3.x engine, rake routes does not work under engine's root (that's why it needs some hack by copying rake file). However rake routes works under test/dummy (or spec/dummy if using rspec). It will list all the pathes which belong to the engine in development and other engines mounted.

Markhor answered 7/12, 2013 at 22:54 Comment(0)
D
3

For rails 3

 desc 'Print out all defined routes inside engine  match order, with names. Target specific controller with CONTROLLER=x.'
  task engine_routes: :environment do
    Rails.application.reload_routes!
    app = ENV['ENGINE'] || "Rails.application"
    all_routes = app.constantize.routes.routes
    require 'rails/application/route_inspector'
    inspector = Rails::Application::RouteInspector.new
    puts inspector.format(all_routes, ENV['CONTROLLER']).join "\n"
  end

Rails 4

 desc 'Print out all defined routes inside engine match order, with names. Target specific controller with CONTROLLER=x.'

  task engine_routes: :environment do
   app = ENV['ENGINE'] || "Rails.application"
   all_routes = app.constantize.routes.routes
   require 'action_dispatch/routing/inspector'
   inspector = ActionDispatch::Routing::RoutesInspector.new(all_routes)
   puts inspector.format(ActionDispatch::Routing::ConsoleFormatter.new, ENV['CONTROLLER'])
  end

Then you can call like

 $rake engine_routes ENGINE="IssueTracker::Engine"
Duster answered 22/1, 2015 at 8:5 Comment(1)
Thanks... from this I was able to figure out how to access the route url_helpers from the engine, for your example "IssueTracker::Engine".constantize.routes.url_helpers with which I could dynamically send to the path helper I needed.Alrzc
C
1

in Rails 5, I could get the routes of the engine using the following command:

bundle exec rake app:routes
Culmiferous answered 7/6, 2017 at 9:23 Comment(0)
U
-1

In rails 3.2X If you are in you "host_app" and have mounted a engine you could probably list the routes by executing (should work out of the box):

bundle exec rake engine_name:routes
Utile answered 29/9, 2015 at 17:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.