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
0
votes
2 answers

`method_missing': undefined local variable or method `development' for AppName::Application:Class (NameError)

Rail version - 4.2.3 When i try to start my app in production environment rails s RAILS_ENV=production I get this error : /.rvm/gems/ruby-2.2.4/gems/railties-4.2.3/lib/rails/railtie.rb:196:in method_missing': undefined local variable or…
Lakhani Aliraza
  • 435
  • 6
  • 8
0
votes
1 answer

All tests throwing TypeError: no implicit conversion of nil into String

UPDATE 20180209: I created a new dummy app with what comes with the initial Rails 5.1.4 install (which includes minitest 5.11.3) and tests completed without issue. Going to continue to experiment with the gemfile to see if I can't narrow down what…
Spectator6
  • 363
  • 1
  • 5
  • 19
0
votes
1 answer

Access a variable in Railtie initializer from other class

I have a gem which has the following Railtie class. I am trying to access my_config variable from outside, i.e another ruby class in the same gem. How can I access it? module myModule module Rails class Railtie < ::Rails::Railtie …
slal
  • 2,657
  • 18
  • 29
0
votes
2 answers

Simple Railtie Extension of Active Record

I'm creating a Rails 3.0.3 gem and can't get it to work: # attached.rb module Attached require 'attached/railtie' if defined?(Rails) def self.include(base) base.send :extend, ClassMethods end module ClassMethods def acts_as_fail …
Kevin Sylvestre
  • 37,288
  • 33
  • 152
  • 232
0
votes
0 answers

How do I swap Rails components with my own? How to swap Rails components in and out?

As far as I understand, what defines a Rails component/module is that it subclasses an own Railtie class from Rails::Railtie (the latter being an interface and being unable of being instantiated.) How do the components/modules get loaded and how do…
von spotz
  • 875
  • 7
  • 17
0
votes
1 answer

NameError: uninitialized constant GemName::Rails::Railtie

I'm creating a gem to encapslate a chunk of functionality from an application. Them gem essentially runs a rake task, but when I run the task with bundle exec rake:assets:precompile, I get the following error rake aborted! Bundler::GemRequireError:…
tonyedwardspz
  • 1,707
  • 2
  • 22
  • 45
0
votes
0 answers

Railties Name Error on production deployment

I'm in the process of deploying my application to production (well, attempting to at least). I keep running into this error: /opt/www/sunstar/shared/bundle/ruby/2.1.0/gems/wicked_pdf-0.11.0/lib/wicked_pdf/railtie.rb:6:in `':…
PSCampbell
  • 858
  • 9
  • 27
0
votes
1 answer

Need to include JavaScript, ERB partial, and a render method from a helper in my own gem

From what I've been able to research online a Rails::Engine would allow me to mimic the Rails paths. I figured this would allow me to include everything just by placing them in the equivalent directories. As I have found the directories aren't…
6ft Dan
  • 2,365
  • 1
  • 33
  • 46
0
votes
1 answer

Rails Railtie initializer does not run

I'm developing a gem for use in a Rails 4 application that include a bit of code to be run as an initializer for the host application. I am adding a class into a module with const_set, but that code isn't ever actually being run. Also, when I put…
732
  • 661
  • 5
  • 12
0
votes
1 answer

Bundler.require fails in clean Rails project

My rails app fails to load when I have the following line in application.rb: Bundler.require(:default, Rails.env) I get the following error: .rvm/gems/ruby-2.0.0-p353@global/gems/railties-4.0.2/lib/rails/initializable.rb:13:in `[]': no implicit…
Gerard
  • 4,818
  • 5
  • 51
  • 80
0
votes
0 answers

Why is older version of sass-rails require higher version of railties (Bundler)

I have in my Gemfile gem 'rails', '4.0.0' gem 'sass-rails', '~> 4.0.0' This works fine. If I run bundle update it says "Using sass-rails (4.0.1)". Now if I try to change it to use 4.0.0 (gem 'sass-rails', '4.0.0') and run bundle update, it gives me…
Chemist
  • 967
  • 2
  • 15
  • 28
0
votes
1 answer

Active admin in Rails 4 throws error "Invalid option key"

Rails 4 app working just fine until Active Admin is included (from this thread) in the gemfile. My Gemfile: gem 'rails', '4.0.0' gem 'rails_12factor', group: :production gem 'pg' gem 'sass-rails', '~> 4.0.0.rc1' gem 'uglifier', '>= 1.3.0' gem…
0
votes
0 answers

Can't generate scaffold. Could not find rail ties gem load error

Ruby/Rails is so confusing. I'm going through the Michael Hartl tutorial, and I get an error with the following terminal command: $ rails generate scaffold Micropost content:string user_id:integer gives…
john
  • 123
  • 2
  • 12
0
votes
1 answer

Heroku Push Error H10

I have been trying for the past couple of days to fix an error that keeps occuring once I have pushed my app to Heroku. The app works fine in development when booted up on Webrick but for some reason I keep receiving an Application Error page when…
Tom Pinchen
  • 2,467
  • 7
  • 33
  • 53
0
votes
1 answer

Rails destroy controller doesn't work

Pretty much there in the title. I worked on a project with someone else, then cloned the git repo to my computer, added a new controller, and then thought better so ran: rails destroy controller requests (requests is the name of the controller),…
Sasha
  • 6,224
  • 10
  • 55
  • 102
1 2
3