<?xml version="1.0" encoding="UTF-8"?>
<post>
  <body>&lt;p&gt;So I&amp;#8217;ve discovered the wonderfully easy-to-use &lt;a href="http://www.thoughtbot.com/projects/paperclip"&gt;Paperclip&lt;/a&gt; attachment plugin for Ruby on Rails, written by Jon Yurek at &lt;a href="http://www.thoughtbot.com"&gt;Thoughbot&lt;/a&gt; .&lt;/p&gt;


	&lt;p&gt;I like that attachments are treated like any other class instance attribute (@post.image), instead of having to be associated with a post from a separate assets resource entirely. It uses Imagemagick, it has a small footprint, many of the features that make attachment_fu great, and others that it lacks.&lt;/p&gt;


	&lt;p&gt;When I started playing around with Paperclip, I noticed that there are some RESTful actions missing in the documentation: destroy, edit, update, and so on. That same documention intimates that you should/can have an upload form on your edit page, so I persevered &#8211; it would really be ideal if users could simply upload new images and documents, overwriting existing ones.&lt;/p&gt;


	&lt;p&gt;When I got to this point, I did what anyone would do: wrote out the actions based on the generated scaffolding patterns, hoping it would &amp;#8216;just work&amp;#8217;. I had a post model with attachments for an image and also a sample text. Not only do I want users to be able to swap these out, but to update other columns in the class instance without losing the existing files attached to the record.&lt;/p&gt;


	&lt;p&gt;When I went ahead a tested the configuration I&amp;#8217;d cobbled together, it wasn&amp;#8217;t working correctly. It didn&amp;#8217;t update, it &lt;em&gt;created&lt;/em&gt; a new record, with the existing values, which was&amp;#8230;not ideal.&lt;/p&gt;


	&lt;p&gt;Evaluating the form_for tag, I noticed that the tag I&amp;#8217;d copied in from the RiDocs was using was a little unusual:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;&amp;lt;% form_for :post, @post, :url =&amp;gt; posts_path, :html =&amp;gt; { :multipart =&amp;gt; true } do |f| %&amp;gt;&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;The tutorials online only require the symbol for the class and the &lt;code&gt;multipart =&amp;gt; true&lt;/code&gt; bits. Not only that, but knowing how RESTful conventions work, that &lt;code&gt;:url =&amp;gt; posts_path&lt;/code&gt; is an obvious problem: when you&amp;#8217;re working with a class instance, your paths should be singular.&lt;/p&gt;


	&lt;p&gt;Changing it to singular (&lt;code&gt;:url =&amp;gt; post_path&lt;/code&gt;) didn&amp;#8217;t work &#8211; suddenly I was getting an error stating that the ID of the object wasn&amp;#8217;t an action! Well, obviously. So I needed to explicitly state the action:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;&amp;lt;% form_for :post, @post, :url =&amp;gt; post_path, :action =&amp;gt; 'update', :html =&amp;gt; { :multipart =&amp;gt; true} do |f| %&amp;gt;  &lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;But this didn&amp;#8217;t work either! The update parameter was either being overlooked or wasn&amp;#8217;t providing the correct parameters in the expected format. Finally, researching the available parameters for the form_for tag, I came up with this:&lt;/p&gt;


&lt;pre&gt;&lt;code&gt;&amp;lt;% form_for :post, @post, :url =&amp;gt; post_path, :html =&amp;gt; { :multipart =&amp;gt; true, :method =&amp;gt; :put } do |f| %&amp;gt;  &lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;Which explicitly states the http method used in the transaction. This works beautifully! File attachments remain the same unless you&amp;#8217;ve selected new ones,  it works for multiple simultaneous attachments on a model&amp;#8230;it&amp;#8217;s gorgeous. I hope this helps, I couldn&amp;#8217;t find much info on edit/update features for Paperclip.&lt;/p&gt;</body>
  <created-at type="datetime">2008-08-29T00:37:23+00:00</created-at>
  <id type="integer">50</id>
  <permalink>edit-update-paperclip-plugin-attachments</permalink>
  <title>Edit/Update Paperclip Plugin Attachments</title>
  <updated-at type="datetime">2008-08-29T07:49:12+00:00</updated-at>
</post>
