2011-01-15 7 views
5

मुझे ऐसा कोई उदाहरण नहीं मिल रहा है जो सभी घटकों में पूर्ण हो। मैं एक मुश्किल समय को हटाने के चित्र अटैचमेंटरेल 3, पेपरक्लिप (और फॉर्मेटास्टिक) - छवि अनुलग्नक हटाना

  1. वर्ग हो रहा है

    class Product 
        has_many :product_images, :dependent => :destroy 
        accepts_nested_attributes_for :product_images 
        end 
    
        class ProductImage 
        belongs_to :product 
        has_attached_file :image #(etc) 
        end 
    
  2. देखें

    <%= semantic_form_for [:admin, @product], :html => {:multipart => true} do |f| %> 
        <%= f.inputs "Images" do %> 
         <%= f.semantic_fields_for :product_images do |product_image| %> 
         <% unless product_image.object.new_record? %> 
          <%= product_image.input :_destroy, :as => :boolean, 
          :label => image_tag(product_image.object.image.url(:thumb)) %> 
         <% else %> 
          <%= product_image.input :image, :as => :file, :name => "Add Image" %> 
         <% end %> 
         <% end %> 
        <% end %> 
        <% end %> 
    
  3. नियंत्रक

    class Admin::ProductsController < AdminsController 
        def edit 
        @product = Product.find_by_permalink(params[:id]) 
        3.times {@product.product_images.build} # added this to create add slots 
        end 
    
        def update 
         @product = Product.find_by_permalink(params[:id]) 
    
         if @product.update_attributes(params[:product]) 
         flash[:notice] = "Successfully updated product." 
         redirect_to [:admin, @product] 
         else 
         flash[:error] = @product.errors.full_messages 
         render :action => 'edit' 
         end 
        end 
        end 
    

अच्छा लगता है, लेकिन, जब मैं चेकबॉक्स चेक करता हूं तो सचमुच कुछ भी नहीं होता है। अनुरोध में मैं देख रहा हूँ:

 "product"=>{"manufacturer_id"=>"2", "size"=>"", "cost"=>"5995.0", 
     "product_images_attributes"=>{"0"=>{"id"=>"2", "_destroy"=>"1"}} 

लेकिन कुछ भी नहीं अद्यतन हो जाता है और उत्पाद छवि सहेजा नहीं गया है।

क्या मुझे 'accepts_nested_attributes_for' काम करने के तरीके के बारे में कुछ मौलिक याद आ रही है? ।

उत्तर

10
ActiveRecord::NestedAttributes::ClassMethods

के लिए एपीआई डॉक्स से

: allow_destroy

अगर सही, नष्ट कर देता है विशेषताओं से किसी भी सदस्यों को एक _destroy कुंजी और कहा कि जैसे 1 सही का आकलन (एक मूल्य के साथ हैश, '1', सच, या 'सच')। यह विकल्प डिफ़ॉल्ट रूप से बंद है।

तो:

accepts_nested_attributes_for :product_images, allow_destroy: true 
+0

अर्घ! हाँ आप सही है! धन्यवाद! पूर्णता के लिए – phil

+0

लाइन होना चाहिए: accepts_nested_attributes_for: product_images,: allow_destroy => true – phil

+0

पूर्ण, फिर: पी – noodl

संबंधित मुद्दे