16

我有一个带有回形针和多个图像的杂物'我实现了 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

我怎样才能修改我的资产表格以像我想要的那样工作?谢谢!

4

1 回答 1

7

The Solution

Hi, here is the solution, the key are how work the nested attributes in 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
于 2012-03-06T04:23:25.237 回答