2

私はモデルの製品と製品の画像を持っています。Product_images はクリップ モデルです。商品には多くの商品画像があります。複数の画像をアップロードし、これらの画像を製品ビュー ページに表示する Active Admin フォームを作成しています。

ただし、製品を保存すると。product テーブルは更新されますが、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"}
4

0 に答える 0