46

In a new rails 4 app I anm getting an unintialized constant error for a module. The module is named ProcessBill and is located in lib/process_bill.rb

console error:

ActionController::RoutingError (uninitialized constant BillsController::ProcessBill):

controller code:

class BillsController < ApplicationController

  include ProcessBill

lib/process_bill.rb

module ProcessBill
markhorrocks
  • 1,199
  • 19
  • 82
  • 151

4 Answers4

78

Have you added lib to your autoload path? This was necessary in Rails 3, I'm not sure if it's still required for Rails 4.

Try adding this into the class definition in config/application.rb -

    config.autoload_paths += %W(#{config.root}/lib)
sevenseacat
  • 24,699
  • 6
  • 63
  • 88
44

I had this problem too with the lib directory with Rails 5 and it appeared in production but not in development. To fix it you need to add the lib directory to eager_load_paths. Here is the relevant part from my application.rb:

config.autoload_paths << "#{Rails.root}/lib"
config.eager_load_paths << "#{Rails.root}/lib"
Peter Marklund
  • 1,033
  • 10
  • 9
  • 8
    This article explains why: http://collectiveidea.com/blog/archives/2016/07/22/solutions-to-potential-upgrade-problems-in-rails-5/ – lucke84 Oct 06 '16 at 15:21
8

This will also work in Rails 5 in your application.rb file:

    config.autoload_paths << Rails.root.join('lib')
    config.autoload_paths << Rails.root.join('lib/notifier')
Wouter Schoofs
  • 966
  • 9
  • 13
5

Try adding this line to the top of your controller:

require "#{Rails.root}/lib/process_bill.rb"
Alyssa Ross
  • 2,169
  • 16
  • 26