2012-05-03 14 views
2

私は単純なレール3.2アプリケーションを作成しようとしています。イメージ(#70365286921100)が必要です、配列がありました(#70365535770260)

は簡単にそれを維持するために、アプリは二つのモデルがあります:

class Product < ActiveRecord::Base 
    has_many :images, :class_name => 'Image' 
end 

class Image < ActiveRecord::Base 
    belongs_to :product 
    has_attached_file :image, :styles => { :normal => "300x300", :small => "70x70" } 
end 

私はここにされactive_adminを使用しています:製品および製品は、多くのイメージを持っている必要があり

イメージなので、ここで私のモデルがあります

:私はフォームを送信すると

<%= semantic_form_for [:admin, @product], :html => {:multipart => true} do |f| %> 

    <%= f.inputs :title, :description, :price %> 

    <%= f.semantic_fields_for :images do |f2| %> 
     <%= f2.file_field :image %> 
    <% end %> 

    <%= f.buttons :commit %> 
    <% end %> 

が、私は次の例外を取得:私のフォームは、製品を作成します

Image(#70365286921100) expected, got Array(#70365535770260) 

{"utf8"=>"✓", 
"authenticity_token"=>"waFPhUIJPD90r5SRVmvvYBEcpZHgFJbM325wZDknWf8=", 
"product"=>{"title"=>"rfrfrf", 
"description"=>"rfrfr", 
"price"=>"200.99", 
"images"=>{"image"=>#<ActionDispatch::Http::UploadedFile:0x007ffe63d19e58 @original_filename="IMG_0097.JPG", 
@content_type="image/jpeg", 
@headers="Content-Disposition: form-data; name=\"product[images][image]\"; filename=\"IMG_0097.JPG\"\r\nContent-Type: image/jpeg\r\n", 
@tempfile=#<File:/var/folders/_j/s1n6_4551cxc765p1zm8w54r0000gq/T/RackMultipart20120503-2609-bwvbis>>}}, 
"commit"=>"Create Product"} 

どうしてですか?誰も助けてくれますか?

ありがとうございます!

答えて

1

あなたの商品モデルにaccepts_nested_attributes_for :imagesが必要と思われます。あなたはparamsハッシュを見れば、あなたが見る

class Product < ActiveRecord::Base 
    has_many :images, :class_name => 'Image' 
    accepts_nested_attributes_for :images 
end 

:製品モデルは次のようになります。何accepts_nested_attributes_for

"images"=>{"image"=> ... 

1対に対応するために、あなたのparamsの構造を変更することですhas_many :images関連で指定された多くの関係。

は、フォームに複数の画像を持っていると仮定すると、あなたのparamsハッシュが含まれます

また
"images_attributes"=>{"0"=>{"image"=> ... }, "1"=>{"image" => ... }, ...} 

を、@productが新しい場合は、ビューに到達する前に、あなたがどこかに@product.images.buildを呼び出すことを確認してください。

関連する問題