Rails3 Engines (mountable apps)...

Hello,

I've a question about Rails 3 Engines. I'm looking everywhere for good
documentation on this subject, but didn't find it.

I'm trying to create a mountable app (embedding a Rails application in
another Rails application). Both the 'base' application and the
'embedded' application are created with rails3.0.0.rc. To behave as
Engine the 'embedded' application has the following code in
'lib/my_engine.rb':

# lib/my_engine.rb
require "my_engine"
require "rails"

module MyEngine
  class Engine < Rails::Engine
  end
end

In the Gemfile of the 'base' application I've included MyEngine as a
gem, using the path directive (to be able to change code without
rebuilding the gem).

# Gemfile
gem "my_engine", :path => "../my_engine"

When I try to start the 'base' application it raises a NameError:

uninitialized constant MyEngine::Application (NameError)

Does anybody have good (Rails3 RC based) documentation on how to create
a mountable app?

And what would be the 'Rails-way' of solving this problem?

Kind regards,

Harm-Jan

H.J. Blok wrote:

Hello,

I've a question about Rails 3 Engines. I'm looking everywhere for good
documentation on this subject, but didn't find it.

I'm trying to create a mountable app (embedding a Rails application in
another Rails application). Both the 'base' application and the
'embedded' application are created with rails3.0.0.rc. To behave as
Engine the 'embedded' application has the following code in
'lib/my_engine.rb':

# lib/my_engine.rb
require "my_engine"
require "rails"

module MyEngine
  class Engine < Rails::Engine
  end
end

In the Gemfile of the 'base' application I've included MyEngine as a
gem, using the path directive (to be able to change code without
rebuilding the gem).

# Gemfile
gem "my_engine", :path => "../my_engine"

When I try to start the 'base' application it raises a NameError:

uninitialized constant MyEngine::Application (NameError)

Does anybody have good (Rails3 RC based) documentation on how to create
a mountable app?

And what would be the 'Rails-way' of solving this problem?

Kind regards,

Harm-Jan

From the code you posted, I don't see a problem.

I would suggest following the structure of each framework and how they
use Railties.

# lib/my_engine.rb
require 'active_support'

module MyEngine
  extend ActiveSupport::Autoload

  autoload :MyClass
  autoload :Whatever
end

# lib/my_engine/engine.rb
require 'my_engine'

module MyEngine
  class Engine < Rails::Engine

  end
end

And now you have 3 different ways to initialize the engine. If you leave
your gem named 'my_engine', you need to put `require 'my_engine/engine'`
in lib/my_engine.rb.
If you rename your gem to 'myengine', you can require the engine inside
of the base app with `require 'my_engine/engine'` inside the bootup or
add another file called lib/myengine.rb that only has `require
'my_engine/engine'` in it.

Samuel Kadolph wrote:

From the code you posted, I don't see a problem.

I would suggest following the structure of each framework and how they
use Railties.

# lib/my_engine.rb
require 'active_support'

module MyEngine
  extend ActiveSupport::Autoload

  autoload :MyClass
  autoload :Whatever
end

# lib/my_engine/engine.rb
require 'my_engine'

module MyEngine
  class Engine < Rails::Engine

  end
end

And now you have 3 different ways to initialize the engine. If you leave
your gem named 'my_engine', you need to put `require 'my_engine/engine'`
in lib/my_engine.rb.
If you rename your gem to 'myengine', you can require the engine inside
of the base app with `require 'my_engine/engine'` inside the bootup or
add another file called lib/myengine.rb that only has `require
'my_engine/engine'` in it.

Thanks for your help, I've tried to implement what you've written, but
somehow it still raises 'uninitialized constant MyEngine::Application
(NameError)'.

I can put my complete test code on github if you like. But at this
moment I have found a working solution, using the 'app2engine' gem
(http://github.com/eregon/app2engine). Benoit Daloze pointed me to that
solution on the Ruby Forum
(http://www.ruby-forum.com/topic/214374#930502) (which I accidentally
used for posting this issue).

HJ

H.J. Blok wrote:

Samuel Kadolph wrote:

From the code you posted, I don't see a problem.

I would suggest following the structure of each framework and how they
use Railties.

<snip>

Thanks for your help, I've tried to implement what you've written, but
somehow it still raises 'uninitialized constant MyEngine::Application
(NameError)'.

Found the problem, the initializers present in 'config/initializers'
caused the 'uninitialized constant' error. My solution for this is to
use 'Rails.application.config...' instead of
'MyEngine::Application.config...' in the 'config/initializers' file(s).

Now deploying it as gem and using it as standalone application both are
possible.

HJ

try update to rc2, then bundle update, and worked for me without
require. (just in gem file defined the engine.)