【问题标题】:activeadmin carrierwave image hint not displayed未显示 activeadmin 载波图像提示
【发布时间】: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


【解决方案1】:

经过一些修改,对我来说工作正常:

ActiveAdmin.register Team do

  permit_params :name, :country :image

  index do
    selectable_column
    column :nome
    column :country
    actions
  end

  form do |f|
    f.inputs "Team" do
      f.input :nome
      f.input :country
      f.input :image, :image_preview => true
    end
    f.actions
  end

  show do
    attributes_table do
      row :nome
      row :country
      row :image do
          image_tag(equipe.image.url)
      end
    end 
  end

end

你可以看到我使用了三个选项: - 索引:显示项目列表 - 形式:为形式 - 显示:显示项目详细信息

这样您就可以自定义所有页面。

【讨论】:

    猜你喜欢
    • 2013-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多