为什么载波会导致NameError:未初始化的常量Micropost :: PictureUploader错误? [英] Why does carrierwave cause the NameError: uninitialized constant Micropost::PictureUploader error?

查看:93
本文介绍了为什么载波会导致NameError:未初始化的常量Micropost :: PictureUploader错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Hartl Rails教程章节 13.4.1基本图像上传中,我们添加了carrierwave gem

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

但是,在生成上载器并将迁移添加到Microposts模型之后,我在测试中仍然遇到此错误:

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

NameError:未初始化的常量Micropost :: PictureUploader

在谷歌搜索后,我能够通过将其添加到我的 environments.rb 中来解决此问题:

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

需要'carrierwave / orm / activerecord'

但是我觉得我做错了,因为Hartl不会这样做,他假设在第13.4章的结尾.1重新启动服务器后您的测试应该通过。

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.

我做错了吗?

UPDATE :

将该行添加到我的 environments.rb 文件后,我的测试通过了。然后,一旦它们通过,我便可以删除该行,并且测试继续通过。就像需要初始化一次的常量一样。

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.

我会说我忘了运行 rails db:migrate 在第一次运行测试之前,但是我继续删除测试数据库,重新创建它,然后运行迁移,重新启动服务器,但它没有修复它。

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.

推荐答案

对此有任何更新吗?我遇到了同样的问题,并通过添加

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


需要'carrierwave / orm / activerecord'

require 'carrierwave/orm/activerecord'

到我的环境。rb。

这篇关于为什么载波会导致NameError:未初始化的常量Micropost :: PictureUploader错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆