How do I get the root directory of a Rails app in a Gem

ruby, ruby-on-rails, rubygems

Solution

The solution that I found to fix a similar problem to this is to include my module using the railtie initializers.

So, in your /lib/mygem/railtie.rb

module MyGem
  class Railtie < Rails::Railtie
    initializer "Include your code in the controller" do
      ActiveSupport.on_load(:action_controller) do
        include MyGem
      end
    end
end

With this code, your module will be included after the ActionController is loaded and you will be able to use Rails.root

Let me know if this works for you.

Problem

Is it possible to know the Rails file system root of an application in the code of a gem included in an app? This is a sample of the gem source: ``` module MyGem def self.included(base) puts Rails.root # return nil end end ActionController::Base.send :include, MyGem ``` Thank's and sorry for my poor english

Original source

Related problems