2013-06-12 4 views
3

제품 양식에 일부 이미지를 저장하려고합니다. 나는 "제품"의 일부인 images_attributes를 가질 수있는 파레 미터를 기대하고 있습니다. 콘솔에서 매개 변수를 만들고 Product를 만들면 이미지가 실제로 저장됩니다.Rails 3.2 has_many : 다형성, Simple Form, simple_fields_for

class Product < ActiveRecord::Base 
    extend FriendlyId 
    friendly_id :name, use: :slugged 

    attr_accessible :description, :name, :category_ids, :brand_ids, :image_ids, :images_attributes 

    has_many :images, :as => :imageable 

    accepts_nested_attributes_for :images 
end 

class Image < ActiveRecord::Base 
    belongs_to :imageable, :polymorphic => true 
    attr_accessible :name, :file 
    mount_uploader :file, ImageUploader 
end 

= simple_form_for(@product, :html => {:multipart => true}) do |f| 
    = f.error_notification 

    .form-inputs 
     = f.input :name 
     = f.input :description 

     = f.association :categories, as: :check_boxes 
     = f.association :brands, as: :check_boxes 

     = f.association :images 

     = simple_fields_for :images do |i| 
      = i.input :file, as: :file 
      = i.input :name 

    .form-actions 
     = f.button :submit 


# GET /products/new 
# GET /products/new.json 
def new 
    @product = Product.new 
    @product.images.build 

    respond_to do |format| 
     format.html # new.html.erb 
     format.json { render json: @product } 
    end 
end 

{ 
    "utf8"=>"✓", 
    "authenticity_token"=>"vvXZFh9sJivA3i4Y0rx9i/oqLwKByrExgYisfdj/N78=", 
    "product"=> { 
     "name"=>"sxsad", 
     "description"=>"saasd", 
     "category_ids"=>[""], 
     "brand_ids"=>[""], 
     "image_ids"=>[""] 
    }, 

    # should be images_attributes and come straight after image_ids? 
    "images"=>{ 
     "name"=>"sdfsdfsdf" 
    }, 

    "commit"=>"Create Product" 
} 

한 번 이미지를 만들면 여러 이미지의 고치처럼 보일 것입니다. 이것이 잘못 될 수있는 곳의 모든 생각은 크게 감사 할 것입니다 :).

답변

2

당신은 간단하게 작성해야 :

= f.simple_fields_for :images do |i| 
+0

아야합니다. 그것은 하나의 멍청한 오타였습니다. 감사합니다 힙, 예상대로 작동합니다! – johan

+0

기쁜 I 've는 도왔다! –