ruby-on-railscarrierwaverailstutorial.org

Why does carrierwave cause the NameError: uninitialized constant Micropost::PictureUploader error?


In the Hartl Rails tutorial chapter 13.4.1 Basic image upload we add the carrierwave gem for image uploads.

But, I kept getting this error in my tests after generating the uploader and adding the migration to the Microposts model:

NameError: uninitialized constant Micropost::PictureUploader

After googling around I was able to fix this by adding this to my environments.rb:

require 'carrierwave/orm/activerecord'

But I feel like I'm doing something wrong because Hartl does not do this and he assumes at the end of chapter 13.4.1 that your tests should be passing after restarting the server.

Am I doing this wrong?

UPDATE:

After adding that line to my environments.rb file my tests passed. Then once they passed, I was able to remove that line and the tests continued to pass. It's like the constant needed to initialized that one time.

I will say that I forgot to run rails db:migrate before running my tests the first time, but I went on to drop the test database, recreate it, and run the migrations, restart the server and it didn't fix it.

Still confused about what went wrong.


Solution

  • Any update on this? I ran into the same issue and it was resolved by adding

    require 'carrierwave/orm/activerecord'

    to my environments.rb.