Lee McAlilly
Lee McAlilly

Reputation: 9316

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.

Upvotes: 2

Views: 1817

Answers (3)

shivamkaushik
shivamkaushik

Reputation: 374

You can try running the Uploader command again and start your server again. Worked for me.

rails generate uploader Picture

Upvotes: 1

Taylor Liss
Taylor Liss

Reputation: 593

BoraMa and user782220's suggestion of running spring stop then rails test worked for me. No need to add the require line to my environments.rb.

I was also running Hartle's tutorial through JetBrains RubyMine on Windows through WSL. I found out that stopping the application in RubyMine didn't actually stop the server. Every time I stopped and started the server, another Puma instance started to run. I had to restart the entire application for all the latent servers to terminate.

You can check if you have excess servers running by typing ps aux | grep puma into your command line.

Seems to be related to this issue

Upvotes: 4

Bryan Reed
Bryan Reed

Reputation: 46

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

require 'carrierwave/orm/activerecord'

to my environments.rb.

Upvotes: 3

Related Questions