纸夹+Active_admin+Rails 3.0.10多张图片

16

我使用了Paperclip作为CRUD的工具,但是当我使用Active Admin进行产品更新时,可以成功更新主图片,但无法上传或编辑多张图片。我的表单如下:

form :html => { :multipart => true } do |f|
  f.inputs "Details" do
    f.input :name
    f.input :created_at, :label => "Publish Product at"
    f.input :category
  end

  f.inputs "Images" do
    f.has_many :assets do |p|
      p.input :asset, :as => :file, :input_html => { :multiple => true }, :label => "Image", :hint => p.object.asset.nil? ? p.template.content_tag(:span, "No Image Yet") : p.template.image_tag(p.object.asset.url(:thumb))
      p.input :_destroy, :as=>:boolean, :required => false, :label=>'Remove'
    end
  end

  f.inputs "Content" do
    f.input :description
  end
  f.buttons
end

并且...

f.inputs "Images" do
    f.has_many :assets do |p|
      p.input :asset, :as => :file, :input_html => { :multiple => true }, :label => "Image", :hint => p.object.asset.nil? ? p.template.content_tag(:span, "No Image Yet") : p.template.image_tag(p.object.asset.url(:thumb))
      p.input :_destroy, :as=>:boolean, :required => false, :label=>'Remove'
    end
  end

我想上传图片,但是当我创建一个新的资源时,它会有一个缺失图片的默认值,并且没有附加正确的图片。我认为这是因为图片路径不正确导致的上传问题。我的资源模型如下:

 class Asset < ActiveRecord::Base
   belongs_to :product
   has_attached_file :asset, :styles => { :large => "340x330", :medium => "140x80>", :thumb => "70x70>" },
     :url => "/products/:id/:style/:basename.:extension",  
     :path => ":rails_root/public/products/:id/:style/:basename.:extension"
 end

我该如何修改我的资产表单以使其按照我想要的方式工作?谢谢!


1
你应该将你的解决方案移到答案并选择你自己的答案,这将把你的问题从未回答的问题列表中移除,并为你赢得另一个徽章。 - Chris Drappier
1个回答

7

解决方案

你好,这里是解决方案,关键在于如何在Formtastic中使用嵌套属性...

 form :html => { :multipart => true } do |f|
   f.inputs "Product information" do
     f.input :name
     f.input :description
   end

   f.inputs "Product images" do
     f.has_many :assets do |p|
       p.input :asset, :as => :file, :label => "Image",:hint => p.object.asset.nil? ? p.template.content_tag(:span, "No Image Yet") : p.template.image_tag(p.object.asset.url(:thumb))
       p.input :_destroy, :as=>:boolean, :required => false, :label => 'Remove image'
     end 
   end

   f.inputs "Product details" do
     f.input :category, :label => "Category", :hint => "Select one category"
     f.input :height
     f.input :width
     f.input :depth
     f.input :color, :label => "Color", :hint => "Select one color"
     f.input :sku, :label => "SKU"
     f.input :price
   end
   f.buttons
 end

你好,我已经尝试了但是出现了“未定义方法`has_many' for nil:NilClass”的错误。 - Richlewis
请查看此链接:http://stackoverflow.com/questions/33081836/multiple-image-upload-in-active-admin-ror。我也尝试了,但是没有成功。 - Harman

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接