31

I read a few documents on these arguments, but I did not understand clearly what they are, what are the differences between them and if one of them fits my needs.

I need to write a piece of application which can be plugged in other application and I want to include it in other applications as a gem. Essentially I need a couple of models, one controller and no views, plus some initialization, support for configuration parameters coming from the hosting app and a generator.

Am I on the right way?

What should I read to understand how to do that?

Update:

A very nice article with a lot af details can be found here.

Essentially:

Railtie is the core of the Rails Framework and provides several hooks to extend Rails and/or modify the initialization process.

A Rails::Engine is nothing more than a Railtie with some initializers already set. And since Rails::Application and Rails::Plugin are engines, the same configuration described here can be used in all three.

Community
  • 1
  • 1
Fabio
  • 18,856
  • 9
  • 82
  • 114
  • I just released my first gem written as a Rails::Engine, it's available on [Github](https://github.com/fabn/google_authentication) thanks for the hint. – Fabio Jul 04 '11 at 17:28
  • Related: http://stackoverflow.com/questions/6118905/rails-3-1-engine-vs-mountable-app/17263429 – Yarin Sep 20 '13 at 03:11

2 Answers2

9

Railtie can probably do what you describe, but it may be more desirable to use an engine. The engine can have its own configuration and also acts like a Rails application, since it allows you to include the /app directory with controllers, views and models in the same manner as a regular Rails app.

Read this blog for more info

River
  • 8,585
  • 14
  • 54
  • 67
johnmcaliley
  • 11,015
  • 2
  • 42
  • 47
  • 1
    Very nice pointer, it does explain a lot of things. Moreover, there is a link with a very interesting document which explain differences even better. I'll update my question to add that link. – Fabio Jun 29 '11 at 15:09
8

Rails::Engine inherits all the functionality from Rails::Railtie and adds some more (Engine < Railtie source code [docs in the source are pretty good]).

Basically, railtie (== your class that inherits from Rails::Railtie) gives you all you need to interact with Rails app processes.
And engine (== your class that inherits from Rails::Engine) is railtie +

  • some initializers set (with help of initializer method): makes your engine's Rails app-like folder structure loadable into the real app, so that

    engine will automatically load app/models, app/controllers, app/helpers into your real app, load routes from config/routes.rb, load locales from config/locales/*, and load tasks from lib/tasks/*.

    You can see initializers set with this code:

    require 'rails/all'
    Rails::Railtie.initializers.map(&:name) #=> []  
    Rails::Engine.initializers.map(&:name)  #=> [:set_load_path, :set_autoload_paths, :add_routing_paths, :add_locales, :add_view_paths, :load_environment_config, :append_assets_path, :prepend_helpers_path, :load_config_initializers, :engines_blank_point]
    
  • some convenience methods, such as isolate_namespace.

Evgenia Karunus
  • 10,715
  • 5
  • 56
  • 70