Rails Paperclip inserting instead of updating -


I have created a simple application that has a product and an image model. The product is in it - many images and images are an attached file feature (paperclip).

I have created a simple_form to create / edit products and it works fine on the creation. However, while editing a product with N images, the rail inserts more N files - empty files.

I have set up a simple form custom input which tests if the image attachment is present in that position rather than changing that location, it provides only one image_tag ().

I see that the HTML has been generated and it shows some weird, hidden tags:

   

And in the Rail Server console I am looking for:

  ProductsController # Updated as HTML Parameter: {"utf8" => "✓ "," Authentication_token "= & gt; "Asdfasdfaasdf =", "Product" = & gt; {"Reference" = & gt; "Y1112CYL.E2", "images_attributes" => {"0" = & gt; {"Id" = & gt; "14"}}}, "comma" = & gt; "Update product", "id" => "20"} Choose Product Load (0.6ms) "Products" * Where "product" from "products" "ID" = $ 1 LIMIT 1 [["id", "20"]] Without Defined Parameters: ID (0.2 ms) BEGIN SQL (1.0 mms) "Image" ("created", "ImageBable_ID", "Imetable_type" , "(" Imageable_id ", 20], [" $ 1, $ 2, $ 3, $ 4] "id" [["created_at", Tuesday, 18 February 2014 05:05:13 UTC + 00: 00] "Imageable_type", "Product"], ["Update_Tit", Tuesday, 18 February 2014 05:05:13 UTC + 00: 00]] (0.6 mms) COMMIT  

Here is the code If anybody can help me for my implementation then I will be very happy! Please forgive me if I If I have left an import part of D, I will gladly edit the question to include it.

_form.html.erb

  <% = Simple_form_for (@product) do | f | & gt; & lt;% = f.error_notification% & gt; & lt; div class = "input" & gt; & lt;% = f.input: reference% & gt; & Lt; h3 & gt; Images & lt; / h3 & gt; & lt; div id = 'image' & gt; & lt;% = f.simple_fields_for: Images | Image | & gt;; & lt;% = 'Image_fields', render: f = & gt; image% & gt; & Lt;% end% & gt; & Lt; Div class = 'link' & gt; & Lt;% = link_to_add_association 'New Image', F ,: Images% & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt; / Div & gt; & Lt; Div class = "verb" & gt; & Lt;% = f.button: Submit% & gt; & Lt; / Div & gt; & Lt;% end% & gt;  

_image_fields.html.erb

  & lt;% = content_tag: div, category: "nested-field image-field"%> & Lt;% = content_tag: div, id: "new-image"%> & Lt;% if f.object.photo.exists? & Gt%; & Lt;% f.template.image_tag (f.object.photo.url (thumb))% & gt; & Lt;% else% & gt; & Lt;% f.input: photo ,: as = & gt; : Photo% & gt; & Lt;% end% & gt; & Lt;% end% & gt; & Lt;% end% & gt;  

App / Input / Photo_Input.ARB

  Class Photo Input & lt; SimpleForm :: Inputs :: FileInput def input out = '' # Output string that we are uploading # if there is an uploaded file (such as: editing mode or form is not saved) if object.send ("# {Attribute_name}? ") # # Preview image to preview # & lt;% = image_tag @ user.avatar.url (: thumb) ,: class = & gt; 'Thumbnail', ID: 'Avatar'% & gt; Outside & lt; & Lt; Template.image_tag (object.send (attribute_name) .url (: thumb) ,: class = & gt; 'thumbnail', id: 'photo') and # attached file input. This is your simple_form rappers (outside & lt; & lt; @ Builder.file_field (attribute_name, input_html_options)). Html_safe End End  

ProductsController # update

  Df Update Reply | Format | | If @ product.update (product_params) format.html {redirect_to @product, notice: 'The product was updated successfully.' } Format.json {head: no_content} else format.html {render action: 'edit'} format.json {render json: @ product.errors, position :: unprocessable_entity} end-of-end  
< / Div>

For future generations, @ Norves' comment has solved it:

I reject all To clear my accepts_nested_attributes_for

  accepts_name_data_for: picture ,: allow_destroy = & gt; Truth: DisclaimerIf = & gt; : All_blank  

Edit: It is revealed that there is a problem with this solution. When reject_if =>: all_blank it is destroyed by working properly. See that an alternative solution shows - that I did not manage to work.


Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -