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

为君一笑 提交于 2020-01-03 19:17:18

问题


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.


回答1:


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

require 'carrierwave/orm/activerecord'

to my environments.rb.




回答2:


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




回答3:


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

rails generate uploader Picture


来源:https://stackoverflow.com/questions/44004065/why-does-carrierwave-cause-the-nameerror-uninitialized-constant-micropostpict

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!