使用CarrierWave在Rails中显示图像的问题

时间:2015-07-05 20:17:12

标签: ruby-on-rails ruby web 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

视图/播放/ _form.html.erb

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

视图/播放/ 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

谢谢!

1 个答案:

答案 0 :(得分:1)

你应该改变

mount_uploaders :profile_images, ImageUploader 

 mount_uploader :profile_image, ImageUploader

请参阅CarrierWave documentation,您刚刚混合了单个和多个文件上传配置。