How can I make routes from a Rails 3 engine available to the host application?

PerfectlyNormal picture PerfectlyNormal · Dec 5, 2010 · Viewed 13.5k times · Source

I have a Rails 3 application with several engines containing additional functionality. Each engine is a separate service that customers can purchase access to.

I am, however, having a problem with routes from the engines that aren't readily available to the controllers and views.

controller:

class ClassroomsController < ApplicationController
  ..
  respond_to :html

  def index
    respond_with(@classrooms = @company.classrooms.all)
  end

  def new
     respond_with(@classroom = @company.classrooms.build)
  end

  ..
end

app/views/classrooms/new.html.haml:

= form_for @classroom do |f|
  ..
  f.submit

config/routes.rb in engine:

MyEngineName::Engine.routes.draw do
  resources :classrooms
end

config/routes.rb in app:

Seabed::Application.routes.draw do
  mount MyEngineName::Engine => '/engine'
  ...
end

lib/my_engine_name.rb in engine:

module MyEngineName
  class Engine < ::Rails::Engine
  end
end

attempting to go to /classrooms/new results in

NoMethodError in Classrooms#new

Showing app/views/classrooms/_form.html.haml where line #1 raised:
  undefined method `hash_for_classrooms_path' for #<Module:0x00000104cff0f8>

and attempting to call classrooms_path from any other view results in the same error. I can, however, call MyEngineName::Engine.routes.url_helpers.classrooms_path and get it working. I'm thinking I might have defined the routes wrong, but can't find another way that works.

Tried running the app with both Passenger (standalone and Apache module) and WEBrick (rails server). Using latest Rails from Git (7c920631ec3b314cfaa3a60d265de40cba3e8135).

Answer

davidrac picture davidrac · Sep 6, 2012

I had the same problem, and found this in the documentation:

Since you can now mount an engine inside application’s routes, you do not have direct access to Engine‘s url_helpers inside Application. When you mount an engine in an application’s routes, a special helper is created to allow you to do that. Consider such a scenario:

# config/routes.rb
MyApplication::Application.routes.draw do
  mount MyEngine::Engine => "/my_engine", :as => "my_engine"
  get "/foo" => "foo#index"
end

Now, you can use the my_engine helper inside your application:

class FooController < ApplicationController
  def index
    my_engine.root_url #=> /my_engine/
  end
end