【发布时间】:2014-04-22 09:06:36
【问题描述】:
您好,我正在使用带有运营商 gem 的活动管理员。我无法在提示中查看图像上传的预览
我的图像 src 始终为空,即 f.template.image_tag(f.object.image.url) 为空
点击上传按钮后,我可以看到图片相关的url。
我的表单是这样的
ActiveAdmin.register Product do
permit_params :name, :description , :category_id , :image
form(:html => { :multipart => true }) do |f|
f.inputs "Product" do
f.input :category_id , :as => :select , :collection => Category.all
f.input :name
f.input :description
f.input :image, :as => :file, :hint => f.template.image_tag(f.object.image.url)
end
f.actions
end
end
模型
class Product < ActiveRecord::Base
belongs_to :category
mount_uploader :image, ImageUploader
end
序列化器
class ProductSerializer < ActiveModel::Serializer
attributes :id, :name, :description , :image
end
我的上传器看起来像这样
class ImageUploader < CarrierWave::Uploader::Base
storage :file
def store_dir
"uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
end
end
生成 HTML
<label class="label" for="product_image">Image</label>
<input id="product_image" name="product[image]" type="file">
<p class="inline-hints"><img src=""></p>
【问题讨论】:
-
您是否检查过浏览器是否提示将您的图像 src 包装在
img标签中,我猜它们仅用于文本 -
yes 在浏览器提示中包含图像 src 标签。我参考了其他 stackoverflow 问题来了解如何以我猜对某些人有用的形式进行预览。
-
您能否向我们展示您的 html 代码以及生成的图像标签以及您拥有 s3 或文件的存储提供商,请同时提及
-
您好,我已经更新了有问题的 html,因为图片上传者建议存储是文件。只是为了检查是否可以显示图像,我用一些随机图像 url 替换了 f.object.image.url 并显示了该图像
标签: ruby-on-rails-4 activeadmin carrierwave