Help us understand the problem. What is going on with this article?

Railsチュートリアル中にNameError: uninitialized constant Micropost::PictureUploaderが出た時の対処法

More than 3 years have passed since last update.

発生した章

13.4.1 基本的な画像アップロードにて、CarrierWaveをインストールして、リスト 13.59: Micropostモデルに画像を追加するの通り修正したら、テストでエラーが出るようになった。

やったこと

CarrierWaveに画像と関連付けたモデルを伝えるために、mount_uploaderメソッドを使用する。引数に、属性名(今回はpicture)と、クラス名(PictureUploader)を指定する。

/sample_app/app/models/micropost.rb
class Micropost < ApplicationRecord
  belongs_to :user
  default_scope -> { order(created_at: :desc) }
  mount_uploader :picture, PictureUploader
  validates :user_id, presence: true
  validates :content, presence: true, length: { maximum: 140 }
end

発生したエラー内容

Micropostモデルを呼び出した際に、以下のエラーが発生する。
NameError: uninitialized constant Micropost::PictureUploader

チュートリアルでは、この時点でのtestはgreenになると書かれているが、redになる。
(Micropostモデルから、mount_uploaderメソッドを削除するとgreenになる)

yokoyan:~/workspace/sample_app (user-microposts) $ rails test
Running via Spring preloader in process 9762
Started with run options --seed 39087

ERROR["test_profile_display", UsersProfileTest, 0.09379067993722856]
 test_profile_display#UsersProfileTest (0.09s)
NameError:         NameError: uninitialized constant Micropost::PictureUploader
            app/models/micropost.rb:4:in `<class:Micropost>'
            app/models/micropost.rb:1:in `<top (required)>'

 FAIL["test_content_should_be_at_most_140_characters", MicropostTest, 2.966606823960319]
 test_content_should_be_at_most_140_characters#MicropostTest (2.97s)
        Expected true to be nil or false
        test/models/micropost_test.rb:32:in `block in <class:MicropostTest>'

 FAIL["test_user_id_should_be_present", MicropostTest, 2.974773451918736]
 test_user_id_should_be_present#MicropostTest (2.97s)
        Expected true to be nil or false
        test/models/micropost_test.rb:22:in `block in <class:MicropostTest>'

 FAIL["test_content_should_be_present", MicropostTest, 2.9872157878708094]
 test_content_should_be_present#MicropostTest (2.99s)
        Expected true to be nil or false
        test/models/micropost_test.rb:27:in `block in <class:MicropostTest>'

 FAIL["test_micropost_interface", MicropostsInterfaceTest, 3.539895322872326]
 test_micropost_interface#MicropostsInterfaceTest (3.54s)
        "Micropost.count" didn't change by 0.
        Expected: 38
          Actual: 39
        test/integration/microposts_interface_test.rb:16:in `block in <class:MicropostsInterfaceTest>'

  59/59: [==================================================================================================================] 100% Time: 00:00:03, Time: 00:00:03

Finished in 3.68161s
59 tests, 245 assertions, 4 failures, 1 errors, 0 skips

参考情報

NameError: uninitialized constant Article::ImageUploader when using Carrierwave on rails 4.1.5

上記の記事を見ると、config/environment.rbの行末に、require 'carrierwave/orm/activerecord'を追記して解決したという人がいる。
他にも、springを再起動したら治ったという人もいる。

対策

springを再起動する。

yokoyan:~/workspace/sample_app (user-microposts) $ spring stop
Spring stopped.
yokoyan:~/workspace/sample_app (user-microposts) $ 
yokoyan:~/workspace/sample_app (user-microposts) $ spring start
Version: 1.7.2

Usage: spring COMMAND [ARGS]

Commands for spring itself:

  binstub         Generate spring based binstubs. Use --all to generate a binstub for all known commands. Use --remove to revert.
  help            Print available commands.
  server          Explicitly start a Spring server in the foreground
  status          Show current status.
  stop            Stop all spring processes for this project.

Commands for your application:

  rails           Run a rails command. The following sub commands will use spring: console, runner, generate, destroy, test.
  rake            Runs the rake command

再度テストを実行。
無事にエラーが解決しました。

yokoyan:~/workspace/sample_app (user-microposts) $ rails test
Running via Spring preloader in process 13599
Started with run options --seed 41861

DEPRECATION WARNING: ActionDispatch::IntegrationTest HTTP request methods will accept only================                   ] 84% Time: 00:00:02,  ETA: 00:00:00
the following keyword arguments in future Rails versions:
params, headers, env, xhr, as

Examples:

get '/profile',
  params: { id: 1 },
  headers: { 'X-Extra-Header' => '123' },
  env: { 'action_dispatch.custom' => 'custom' },
  xhr: true,
  as: :json
 (called from block (2 levels) in <class:MicropostsInterfaceTest> at /home/ubuntu/workspace/sample_app/test/integration/microposts_interface_test.rb:25)
  59/59: [==================================================================================================================] 100% Time: 00:00:03, Time: 00:00:03

Finished in 3.52173s
59 tests, 262 assertions, 0 failures, 0 errors, 0 skips
yokoyan
SIerで働くエンジニアです。自社WEBサービスの開発・AWSのインフラ構築を行っています。なれる最高のフルスタックエンジニアを目指して、RailsやAWSをこつこつ学習中。 本を10冊出すことが夢です。 「やさしいT-SQL入門」(翔泳社)および、「基礎からわかる PL/SQL」(シーアンドアール研究所)の著者。
http://www.yokoyan.net/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away