Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Scoped engines routing

#5287

Right now, there is no control over engine routes that are injected into base app. It is impossible to mount engine at "/foo/" instead of "/"

However it could be easily done with 9 lines of code.

# Some hacking
class Rails::Engine
  def self.define_routes(&block)
    @@__routes_block = block
  end
  
  def self.draw_routes(where)
    where.instance_exec &@@__routes_block
  end
end

# then

# my_app/config/routes.rb
MyApp::Application.routes.draw do |map|
  scope "bar" do
    MyEngine::Engine.draw_routes(self)
  end
end

# my_engine.rb

require "rails"

module MyEngine
  class Engine < Rails::Engine
    define_routes do
      match "/my_engine", :to => "my_engine#foo"
    end
  end
end


# $ rake routes
# bar_my_engine  /bar/my_engine {:controller=>"my_engine", :action=>"foo"}

Reported by teamon · August 3rd, 2010 @ 04:30 PM

State: wontfix
Milestone: none
Assigned to: nobody
Importance: Low

Activity

  1. Andrew White
    Andrew White
    • State changed from new to wontfix
    • Importance changed from to Low

    I think that it's best to leave route customization to the engine itself. An engine may make assumptions about it's routes and if they're moved it may break the engine. If you feel this decision is incorrect please raise the issue on the rubyonrails-core mailing list.

    August 20th, 2010 @ 10:41 AM