Questions tagged [railtie]

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

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

Every major component of Rails (Action Mailer, Action Controller, Action View and Active Record) is a Railtie. Each of them is responsible for their own initialization. This makes Rails itself absent of any component hooks, allowing other components to be used in place of any of the Rails defaults.

Developing a Rails extension does not require any implementation of Railtie, but if you need to interact with the Rails framework during or after boot, then Railtie is needed.

For example, an extension doing any of the following would require Railtie:

  • creating initializers
  • configuring a Rails framework for the application, like setting a
    generator
  • +adding config.*+ keys to the environment
  • setting up a subscriber with ActiveSupport::Notifications
  • adding rake tasks

Creating your Railtie

To extend Rails using Railtie, create a Railtie class which inherits from Rails::Railtie within your extension’s namespace. This class must be loaded during the Rails boot process.

The following example demonstrates an extension which can be used with or without Rails.

# lib/my_gem/railtie.rb
module MyGem
  class Railtie < Rails::Railtie
  end
end

# lib/my_gem.rb
require 'my_gem/railtie' if defined?(Rails)

Initializers

To add an initialization step from your Railtie to Rails boot process, you just need to create an initializer block:

class MyRailtie < Rails::Railtie
  initializer "my_railtie.configure_rails_initialization" do
    # some initialization behavior
  end
end

If specified, the block can also receive the application object, in case you need to access some application specific configuration, like middleware:

class MyRailtie < Rails::Railtie
  initializer "my_railtie.configure_rails_initialization" do |app|
    app.middleware.use MyRailtie::Middleware
  end
end

Finally, you can also pass :before and :after as option to initializer, in case you want to couple it with a specific step in the initialization process.

Configuration

Inside the Railtie class, you can access a config object which contains configuration shared by all railties and the application:

class MyRailtie < Rails::Railtie
  # Customize the ORM
  config.app_generators.orm :my_railtie_orm

  # Add a to_prepare block which is executed once in production
  # and before each request in development
  config.to_prepare do
    MyRailtie.setup!
  end
end

Loading rake tasks and generators

If your railtie has rake tasks, you can tell Rails to load them through the method ::rake_tasks:

class MyRailtie < Rails::Railtie
  rake_tasks do
    load "path/to/my_railtie.tasks"
  end
end

By default, Rails load generators from your load path. However, if you want to place your generators at a different location, you can specify in your Railtie a block which will load them during normal generators lookup:

class MyRailtie < Rails::Railtie
  generators do
    require "path/to/my_railtie_generator"
  end
end

Application and Engine

A Rails::Engine is nothing more than a Railtie with some initializers already set. And since Rails::Application is an engine, the same configuration described here can be used in both.

Be sure to look at the documentation of those specific classes for more information.

From http://edgeapi.rubyonrails.org/classes/Rails/Railtie.html

45 questions
360
votes
15 answers

How to define custom configuration variables in Rails?

I was wondering how to add custom configuration variables to a Rails application and how to access them in the controller? Secondly, I was planning to have S3 support for uploads in my application, if I wanted to add a yaml file with the S3 access,…
Shiv
  • 8,362
  • 5
  • 29
  • 32
45
votes
6 answers

Bundler could not find compatible versions for gem “railties” for Rails 4.0.0

I am trying to upgrade to Rails 4.0.0, and I changed the gem versions of sass-rails and coffee-rails. I need to resolve this gem conflict between rails and coffee-rails before I can upgrade to Rails 4. When I ran bundle update this is the output I…
Brian Petersen
  • 491
  • 1
  • 4
  • 5
44
votes
3 answers

cannot load railtie after upgrade to rails 4 per ruby.railstutorial.org

OS is Ubuntu 12.04, 64 bit. New to rails. Relatively new to Ruby. Following the ruby.railstutorial.org tutorial, chapter 3. The tutorial has been updated to use ruby 2.0.0 and Rails 4.0.0.rc1. Previously the gemfile was specifying Rails 3.2.13 and…
Paul S
  • 1,424
  • 1
  • 14
  • 12
31
votes
2 answers

Differences between railties and engines in Ruby On Rails 3

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…
Fabio
  • 18,856
  • 9
  • 82
  • 114
9
votes
2 answers

How to trigger Railtie initializers in my tests?

I have my own gem, and my railtie looks like... class MyRailtie < Rails::Railtie initializer "my_railtie.configure_rails_initialization" do # some initialization behavior end end and I'm trying to test it, but in the tests the initializer…
Steven Barragán
  • 1,144
  • 1
  • 11
  • 21
8
votes
3 answers

Previewing Mailers on non-development tiers

I have several mailer previews under spec/mailer/previews. On development I can view all the previews under /rails/mailers/. However by default this functionality does not exist on other environments. I wanted to enable it on the staging environment…
user2490003
  • 10,706
  • 17
  • 79
  • 155
7
votes
1 answer

Why Railtie initializers are not executed?

While crafting the Passenger-Monit plugin, I thought that it'll be most appropriate to use the initializer, i.e. module PassengerMonit class Railtie < Rails::Railtie initializer "my_plugin.some_init_task" do # my initialization tasks …
Roman
  • 13,100
  • 2
  • 47
  • 63
6
votes
1 answer

mobile_fu for Rails 4

I'm trying to switch my app from Rails 3.2.13 to Rails 4. In doing so, I've hit one main snag -- I use the gem mobile_fu in order to determine if users are coming from a mobile device. That gem requires railties 3.2.13, but rails 4.0.2 and…
Ringo Blancke
  • 2,444
  • 6
  • 30
  • 54
5
votes
1 answer

Railtie Initializer not running in plugin

I recently switched from the gem version of resources_controller to a plugin as the gem version relied on git. Inside the vendor/plugins/plugin/lib/plugin.rb file, the Railtie is as follows: module Ardes module ResourcesController class…
5
votes
1 answer

Railtie: How to access initializer and lib loading hooks?

I am developing a gem for my Rails application, which be loaded into it through Railtie. I'm basically inserting models into into, plus libraries and a few initializers, in the old Rails app style. My main concern is not knowing exactly in the whole…
ChuckE
  • 5,610
  • 4
  • 31
  • 59
4
votes
2 answers

Disable a gem's Railtie initializer

Is there a way to disable a railtie that is loaded by a gem by default ? The developers of the gem did not make it modular and once putting the gem in the Gemfile, the require will automatically load the railties this way: require 'some_gem' module…
Cyril Duchon-Doris
  • 12,964
  • 9
  • 77
  • 164
4
votes
2 answers

Rails::Railtie: Trouble creating a Rails 3 gem

I could really use another set of eyes on this so I thought I would post it here. A while ago I wrote a basic ActiveRecord Extension for my own educational purposes. I've been reading about Railties lately and thought I would try to get it working…
mikewilliamson
  • 24,303
  • 17
  • 59
  • 90
4
votes
1 answer

Inject route from gem to rails 3 routes.rb using Railties (devise issue)

I would like to automatically inject a route from my gem to the host app using a railtie. So far I have something like this: module Fabes class FabesRailtie < ::Rails::Railtie ActionController::Base.send :include, Fabes::Helper …
fuzzyalej
  • 5,903
  • 2
  • 31
  • 49
3
votes
0 answers

Error when trying to inherit from ActionView::Template::Handler in Rails 3.2.13

I tried upgrading from Rails 3.0.23 to 3.2.13 and now I have an error stopping the server from starting that looks like this: /Users/hamiltonchapman/Projects/project-static/lib/markdown_handler.rb:3:in `': uninitialized constant…
hamchapman
  • 2,901
  • 4
  • 22
  • 37
3
votes
2 answers

Railtie(gem): How do i include and render erb partials?

I'm working on a Railtie(gem) for embedding videos like YouTube, Vimeo etc. In this gem i want to have a view helper, so that i can call embed_video(embeddable, width, height) So i have created the helper as a helper, and it works, but i would like…
Pål
  • 958
  • 2
  • 13
  • 18
1
2 3