code-gijoe
code-gijoe

Reputation: 7234

What is this uninitialized constant error?

I created ruby class and put it in app_dir/lib/appointment_messaging_job.rb

class AppointmentMessagingJob
  def perform
  end
end

In one of my controllers I do this:

test = AppointmentMessagingJob.new

I get a very annoying very incomprehensible error:

Started GET "/en/appointments/1/approve" for 127.0.0.1 at 2012-09-04 13:02:43 -0400
Processing by AppointmentsController#approve as HTML
  Parameters: {"locale"=>"en", "id"=>"1"}
Completed 500 Internal Server Error in 2ms

NameError (uninitialized constant AppointmentsController::AppointmentMessagingJob):
  app/controllers/appointments_controller.rb:89:in 'approve'

What is this uninitialized constant? I really don't get it.

Upvotes: 1

Views: 2034

Answers (1)

Dave Newton
Dave Newton

Reputation: 160191

Your library isn't being loaded and it's failing to resolve the class name.

Update your config/application.rb to automatically include the lib directory; there is a line in there that is commented out by default:

# Custom directories with classes and modules you want to be autoloadable.
# config.autoload_paths += %W(#{config.root}/extras)

(You'd want to include the lib directory, not extras, obviously.)

IIRC you could also use require 'test_class' in your controller, the lib directory is on the library path. This makes the dependency more explicit, but I'm not sure it's that much more communicative.

Upvotes: 4

Related Questions