我有模型产品和产品图片。 Product_images是一种回形针模型。产品有很多product_images。我正在制作一个Active Admin表单,用于上传多个图像并将这些图像显示在Products视图页面中。
然而,当我保存产品时。产品表已更新,但不是product_image表。没有保存图像。
class Product < ActiveRecord::Base
attr_accessible :name, :product_images_attributes
has_many :product_images, :dependent => :destroy
accepts_nested_attributes_for :product_images, :reject_if => lambda { |t| t['product_image'].nil? }, :allow_destroy => true
end
class ProductImage < ActiveRecord::Base
attr_accessible :name
attr_accessible :image
belongs_to :product
has_attached_file :image, :styles => { :small => "150x150>", :large => "320x240>" }
validates_attachment_presence :image
end
ActiveAdmin.register Product do
form :html => { :multipart => true } do |f|
f.inputs "Admin Details" do
f.input :name
end
f.inputs "Product images" do
f.has_many :product_images do |p|
p.input :image, :as => :file, :label => "Image",:hint => p.object.image.nil? ? p.template.content_tag(:span, "No Image Yet") : p.template.image_tag(p.object.image.url(:small))
p.input :_destroy, :as=>:boolean, :required => false, :label => 'Remove image'
end
end
f.buttons
end
端
这是控制台输出:
Processing by Admin::ProductsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"J0JD6esuv2P5Xvt7N3drh8MSZRQD3gSUydF/ly7qjGA=", "product"=>{"name"=>"Clark Shoes 1", "product_images_attributes"=>{"1341259410694"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x0000010673de38 @original_filename="500x_jello.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"product[product_images_attributes][1341259410694][image]\"; filename=\"500x_jello.jpg\"\r\nContent-Type: image/jpeg\r\n", @tempfile=#<File:/var/folders/ho/hot88oWKF1qEWoeAWQPvUE+++TI/-Tmp-/RackMultipart20120702-415-8m58iu>>, "_destroy"=>"0"}}}, "commit"=>"Update Product", "id"=>"1"}