Listing 'rake routes' for a mountable Rails 3.1 engine

流过昼夜 提交于 2019-11-28 23:09:07

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

$ rake app:routes

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.

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.

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"

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

bundle exec rake app:routes

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!