使用 CarrierWave 在 Rails 中显示图像时出现问题

Issues displaying images in Rails using CarrierWave

我在显示上传图片的缩略图时遇到问题。一个文件成功上传,但我一直无法显示它。任何查找 url 或路径的尝试都会引发未定义的方法错误。非常感谢所有帮助!

型号

class Play < ActiveRecord::Base
  mount_uploaders :profile_images, ImageUploader
end

控制器

...
def play_params
    params.require(:play).permit(:title, :description, :date_of_play,:profile_image)
end
...

上传者

class ImageUploader < CarrierWave::Uploader::Base
  include CarrierWave::RMagick
  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  version :thumb do
   process :resize_to_limit => [200, 200]
  end

  def extension_white_list
   %w(jpg jpeg gif png)
  end
end

views/plays/_form.html.erb

...
<%= f.file_field :profile_image %>
...

views/plays/show.html.erb

...
<%= image_tag @play.profile_image.url(:thumb) if @play.profile_image.present? %>
...

迁移

class CreatePlays < ActiveRecord::Migration
 def change
  create_table :plays do |t|
    t.string :title, null: false
    t.string :description, null: false
    t.datetime :date_of_play, null: false
    t.string :profile_image

    t.timestamps null: false
  end
 end
end

错误

undefined method `url' for "#<File:0x00000005a7dbb8>":String

谢谢!

你应该改变

mount_uploaders :profile_images, ImageUploader 

 mount_uploader :profile_image, ImageUploader

参考CarrierWave documentation,您刚刚混淆了单个和多个文件上传配置。