未初始化的常量 Car::PictureUploader

uninitialized constant Car::PictureUploader

我正在尝试在 app/uploaders 中添加图片上传器,但在加载我的应用程序时出现此错误:

uninitialized constant Car::PictureUploader on line 3 of car.rb

app/models/car.rb

class Car < ActiveRecord::Base
  belongs_to :user
  mount_uploader :picture, PictureUploader
  validates :user_id, presence: true
  validates :year, presence:true, length: { maximum: 4 }
  validates :brand, presence:true

app/uploaders/picture_uploader.rb

class PictureUploader < CarrierWave::Uploader::Base
  storage :file

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  # Add a white list of extensions which are allowed to be uploaded.
  def extension_white_list
    %w(jpg jpeg gif png)
  end
end

我以前遇到过这个错误,因为相当于 picture_uploader.rb 的文件不在正确的文件夹中,但我认为这里一切正常。当我在 user.rb 中注释掉第 3 行时,页面加载。知道如何让我的页面进入 运行 吗?

我想你想要:

::PictureUploader

它指定它只是 class PictureUploader 而不是命名空间 Car.

中的 class PictureUploader

出现同样的错误,不得不手动终止服务器进程:

ps aux | grep server
kill [server process id]

出于某种原因,之前使用 Ctrl+C 关闭服务器似乎没有用。

只需重新启动您的服务器。它会解决这个问题。你的代码一切正常。