Search code examples
ruby-on-railsrubymixins

How is this an un-initialized constant. (Mixins in rails)


under lib/ I have 'aisis_writer/loader.rb' which, inside that file, looks like:

module AisisWriter
  module Loader
  end
end

I then have, in my application.rb the following set up:

require File.expand_path('../boot', __FILE__)

# Pick the frameworks you want:
require "active_record/railtie"
require "action_controller/railtie"
require "action_mailer/railtie"
require "sprockets/railtie"
# require "rails/test_unit/railtie"

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env)

module AisisWriter
  class Application < Rails::Application
    # Load Lib
    config.autoload_paths += %W(#{config.root}/lib)

    # Use Rack Attack For Throttling
    config.middleware.use Rack::Attack
  end
end

From there I did, in the ApplicationController.rb: include AisisWriter::Loader and then I ran my tests and got:

'<class:ApplicationController>': uninitialized constant AisisWriter::Loader (NameError)

Either I cannot do what I am doing because of naming conflicts or I am doing something wrong. Any one care to tell me what I might be doing wrong?


Solution

  • I don't think your config.autoload_paths is broad enough -- it's not including subfiles of the lib directory.

    This should do the trick:

    config.autoload_paths += Dir[Rails.root.join('lib', '{**/}')]