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
3
votes
1 answer

uninitialized constant in My::Engine after file change

I'm developing a gem/engine. The way I do this is by bundling it in a test RailsApp from source: # Gemfile gem 'my-engine', path: '../local/path/to/gem' This works fine so far. But, after I change a file in my gem (add a space or a break for…
Tim Baas
  • 6,035
  • 5
  • 45
  • 72
2
votes
2 answers

Heroku Deploy: URI::InvalidURIError: bad URI(is not URI?): ://user:pass@127.0.0.1/dbname

I have a project with a Vue frontend and a rails backend. It's just a very simple API on the backend, no database or anything. It's working fine locally but now I want to deploy it on Heroku. However, when I run it I get the following error. ----->…
Chris A
  • 863
  • 1
  • 10
  • 31
2
votes
1 answer

Controlling Rails Initializer Load Order (Possible Need for new Rails Initialization Hook)

I'm building a Rails Engine which uses OmniAuth. OmniAuth needs to add some middleware to the Rails middleware stack and a suggested way to do this, according to OmniAuth, is to do it in an initializer. I've tried it, and I'm successfully able to…
CodeSmith
  • 2,133
  • 1
  • 20
  • 43
2
votes
0 answers

Running initialization only once

I'm working on a Rails app that needs to execute some code when it boots. Since I want to share this behavior with future apps, I have put this code into a gem using a Railtie class. lib/my_gem/railtie.rb module MyGem class Railtie <…
Sig
  • 5,476
  • 10
  • 49
  • 89
1
vote
1 answer

Altering the current outcome of the model of a generated scaffold

I aim to make some turbo additions to the current scaffold generator. For that i need the plural_model_name in the model. I am looking for a way to alter the output model, generated by the rails g scaffold command. class <%= singular_model_name %> <…
1
vote
0 answers

Rails 3.2.25 upgrade to 4.0.0 - "uninitialized constant ActiveRecord" on all active record queries

I'm in the middle of performing an edge ruby and rails update for a web app built in rails 2.3.18 and running ruby 1.9.3. I have been incrementally been updating the rails version and fixing breaks and things have been moving forward at a steady…
Verty00
  • 726
  • 5
  • 16
1
vote
1 answer

Why do modern rails plugin gems favor config initializers over Railtie's config hook?

I used to see a lot of gems where you would configure them in your application.rb file or one of the environment files because they hooked into Railtie's configuration helpers. But now it seems every gem I use handles configuration by having an…
Xavier
  • 3,423
  • 23
  • 36
1
vote
1 answer

Ruby On Rails throws error on home - railties

When I try my page i.e, localhost:3000/home - it shows this error. This is the error thrown in the console: ActionController::RoutingError (No route matches [GET] "/home"): web-console (2.0.0.beta3) lib/action_dispatch/debug_exceptions.rb:22:in…
Sreeraj Chundayil
  • 5,548
  • 3
  • 29
  • 68
1
vote
0 answers

rails: get uninitialized constant ActiveSupport::Logger::SimpleFormatter everytime I try to run my specs

I use both rake spec and rspec spec/ trying to run my rspec tests, but I get the following error: /home/XXX/.rvm/gems/ruby-2.2.1/gems/railties-4.2.1/lib/rails/application/configuration.rb:49:in `initialize': uninitialized constant…
benams
  • 4,308
  • 9
  • 32
  • 74
1
vote
1 answer

Best way to write initializers on a gem

I'm writing my first gem, and I've some issues with initializers part. So I read about Railtie, and I'm little bit confused, there is part that talks about initializers and something on generators. According to this thread, he suggested using…
Yarin Gold
  • 489
  • 1
  • 4
  • 17
1
vote
2 answers

Cannot resolve gem dependencies - doorkeeper, sass-rails - on jruby using Rails 4

I have googled around a lot to find answers and cannot find one. How do I resolve this dependency issue. If I remove sass-rails (which isn't practical but just for testing issues) then the problem just rolls down the Gemfile to coffee-rails having…
Richard Jordan
  • 8,066
  • 3
  • 39
  • 45
1
vote
1 answer

Why Rails::Application has many duplicate initializers which are defined in Rails::Engine?

As i was working throught the rails 3 initialization process, i found that all the initializers defined in Rails::Engine(there are 10) were added to Rails::Application instance more than once. This means that these initializers would run many times.…
tomwang1013
  • 1,349
  • 2
  • 13
  • 27
1
vote
1 answer

Determine which Rails version I'm using when using Railties

Stupid question: I'm using Railties version 3.2.3 and wish to check which version of Rails its running exactly (what with the recent number of security issues). I've looked into the gem's code but can't seem to get a handle on it. Perhaps I'm being…
Gerard
  • 4,818
  • 5
  • 51
  • 80
1
vote
1 answer

Could not find RubyGem railties (>= 0) (Gem::LoadError) with the RailsInstaller

In Ruby on Rails, I am trying to install it on my Windows machine following the step-by-step instructions indicated in http://railsinstaller.org/windows On step 7, it says we should type in this command: $ rails g controller welcome index And so…
Xar
  • 7,572
  • 19
  • 56
  • 80
1
vote
1 answer

rails gem incompatibility clearance & railties, what should I do?

I have rails 3.017, and clearance 1.0.0.rc2 and trying to install gem 'jquery-rails', '~> 2.1' Do I need an older version of jquery-rails? If so, how can I figure this out? thanks Joel Bundler could not find compatible versions for gem "railties": …
Joelio
  • 4,621
  • 6
  • 44
  • 80