0
我遇到顯示我上傳圖像縮略圖的問題。一個文件成功上傳,但我一直無法顯示它。任何嘗試查找url或路徑都會引發未定義的方法錯誤。真的很感謝任何和所有的幫助!使用CarrierWave在Rails中顯示圖像的問題
模型
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
謝謝!
'mount_uploaders:profile_images,ImageUploader'應該是'mount_uploader:profile_image,ImageUploader'。 –
哦,我的上帝。你SAINT!請回答我的問題,以便我可以接受它,並給出一些甜蜜點。我以前嘗試更改profile_images - > profile_image,但從來沒有想過關於mount_uploaders-> mount_uploader –