<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Burm.net &#187; paperclip</title>
	<atom:link href="http://burm.net/tag/paperclip/feed/" rel="self" type="application/rss+xml" />
	<link>http://burm.net</link>
	<description>John Burmeister's Blog</description>
	<lastBuildDate>Tue, 31 Jan 2012 03:31:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Ruby On Rails Polymorphic Paperclip Plugin Tutorial</title>
		<link>http://burm.net/2008/10/17/ruby-on-rails-polymorphic-paperclip-plugin-tutorial/</link>
		<comments>http://burm.net/2008/10/17/ruby-on-rails-polymorphic-paperclip-plugin-tutorial/#comments</comments>
		<pubDate>Sat, 18 Oct 2008 02:59:48 +0000</pubDate>
		<dc:creator>John Burmeister</dc:creator>
				<category><![CDATA[My Projects]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[paperclip]]></category>
		<category><![CDATA[RoR]]></category>

		<guid isPermaLink="false">http://burm.net/?p=138</guid>
		<description><![CDATA[This tutorial is an extension of the paperclip tutorial I put up last week. This time we are going to take advantage of polymorphic paperclip. Polymorphic paperclip utilizes separate tables (an assets table and an attachings table) to track your attachments. This allows for an unlimited number of attachments per item per model. In the [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fburm.net%2F2008%2F10%2F17%2Fruby-on-rails-polymorphic-paperclip-plugin-tutorial%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fburm.net%2F2008%2F10%2F17%2Fruby-on-rails-polymorphic-paperclip-plugin-tutorial%2F&amp;source=burmjohn&amp;style=compact&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>This tutorial is an extension of the <a href="http://burm.net/2008/10/07/the-ruby-on-rails-paperclip-plugin-tutorial-easy-image-attachments/">paperclip tutorial</a> I put up last week. This time we are going to take advantage of polymorphic paperclip.  Polymorphic paperclip utilizes separate tables (an assets table and an attachings table) to track your attachments.  This allows for an unlimited number of attachments per item per model.  In the first tutorial I showed you how to attach just one item, although you could add more columns to your table to handle additional attachments this way is a lot more flexible.  While this plugin is not perfect, there is a lot of room for improvement, its a great starting point.  Perhaps a fork is on its way from me&#8230; </p>
<p>Project Page: <a href="http://github.com/heavysixer/paperclippolymorph/tree/master">http://github.com/heavysixer/paperclippolymorph/tree/master</a></p>
<p><strong>First things first, lets install the plugin:</strong></p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">script<span style="color:#006600; font-weight:bold;">/</span>plugin git:<span style="color:#006600; font-weight:bold;">//</span>github.<span style="color:#9900CC;">com</span><span style="color:#006600; font-weight:bold;">/</span>heavysixer<span style="color:#006600; font-weight:bold;">/</span>paperclippolymorph.<span style="color:#9900CC;">git</span></pre></div></div>

<p>Lets setup our new migration that is needed.  After reading the rdoc, I noticed there was a new generator installed. Great, this makes things even easier.  Lets verify that the generator is available for use, first run:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">script<span style="color:#006600; font-weight:bold;">/</span>generate</pre></div></div>

<p>This should produce a list of all generators available for use, my list below might vary from yours depending upon what plugins you have installed&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">Installed Generators
  Plugins (vendor/plugins): authenticated, forgot_password, open_id_authentication_tables, paperclip, polymorphic_paperclip, roles, rspec, rspec_controller, rspec_model, rspec_scaffold, upgrade_open_id_authentication_tables
  Builtin: controller, integration_test, mailer, migration, model, observer, plugin, resource, scaffold, session_migration</pre></div></div>

<p>As you will notice there is a polymorphic_paperclip generator, go ahead an run it as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">script<span style="color:#006600; font-weight:bold;">/</span>generate polymorphic_paperclip</pre></div></div>

<p>Before you go and run rake db:migrate, open up the migration and modify the assets_count to attachings_count &#8211; it seems there is a minor bug.  I notified the author of the plugin and will submit a patch via git.  Now its OK to run rake db:migrate</p>
<p>Now we have the migration generated and the tables have been added to your database. Next we have to add (or change if you already have paperclip setup) the model where you want to have attachments by adding  acts_as_polymorphic_paperclip.  As an example, I posted my documents model below.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Document <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#CC00FF; font-weight:bold;"><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span></span>
<span style="color:#008000; font-style:italic;"># Document Belongs To A User</span>
<span style="color:#5A0A0A; font-weight:bold;">belongs_to</span> <span style="color:#ff3333; font-weight:bold;">:user</span>
<span style="color:#008000; font-style:italic;"># for paperclip (polymorphic)</span>
acts_as_polymorphic_paperclip 
<span style="color:#008000; font-style:italic;"># Validations</span>
...</pre></div></div>

<p><strong>Note: </strong> Ideally I would like to be able to override the styles settings that are set in the plugins assets.rb, however I went ahead and hard coded them there to fit my needs. Example:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Asset <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#CC00FF; font-weight:bold;"><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span></span>
  <span style="color:#5A0A0A; font-weight:bold;">has_many</span> <span style="color:#ff3333; font-weight:bold;">:attachings</span>, <span style="color:#ff3333; font-weight:bold;">:dependent</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> :<span style="color:#5A0A0A; font-weight:bold;">destroy</span>
  has_attached_file <span style="color:#ff3333; font-weight:bold;">:data</span>,
                    <span style="color:#ff3333; font-weight:bold;">:styles</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>
                    <span style="color:#ff3333; font-weight:bold;">:thumb</span><span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;100x100#&quot;</span>,
                    <span style="color:#ff3333; font-weight:bold;">:small</span>  <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;150x150&gt;&quot;</span>,
                    <span style="color:#ff3333; font-weight:bold;">:medium</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;300x300&gt;&quot;</span>,
                    <span style="color:#ff3333; font-weight:bold;">:large</span> <span style="color:#006600; font-weight:bold;">=&gt;</span>   <span style="color:#996600;">&quot;500x500&gt;&quot;</span>,
                    <span style="color:#ff3333; font-weight:bold;">:xlarge</span> <span style="color:#006600; font-weight:bold;">=&gt;</span>   <span style="color:#996600;">&quot;600x600&gt;&quot;</span>,
                    <span style="color:#ff3333; font-weight:bold;">:xxlarge</span> <span style="color:#006600; font-weight:bold;">=&gt;</span>   <span style="color:#996600;">&quot;800x800&gt;&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>For now, lets move onto the views where you are going to allow attachments.  We are just going to have one upload, perhaps in another tutorial we will look at handling multiple uploads.  </p>
<p>You need to make sure you put the html => { :multipart => true } in both your edit and new views for the model you are working with. Example in my case:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#5A0A0A; font-weight:bold;">form_for</span><span style="color:#006600; font-weight:bold;">&#40;</span>@document,:html <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:multipart</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">error_messages</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#5A0A0A; font-weight:bold;">render</span> <span style="color:#ff3333; font-weight:bold;">:partial</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'form'</span>, <span style="color:#ff3333; font-weight:bold;">:locals</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:f</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> f <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>Next you need to add the file upload field to your _form or edit/new views.</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">&lt;p&gt;
  Attach a file or image &lt;br /&gt;
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#5A0A0A; font-weight:bold;">file_field</span> <span style="color:#ff3333; font-weight:bold;">:data</span><span style="color:#006600; font-weight:bold;">%&gt;</span>
&lt;/p&gt;</pre></div></div>

<p>Now lets go ahead and make these attachments viewable in the document.  For the edit view I added the following:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">&lt;p&gt;<span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#5A0A0A; font-weight:bold;">image_tag</span> asset.<span style="color:#9900CC;">url</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:medium</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;/p&gt;
&lt;p&gt;Tiny:  <span style="color:#006600; font-weight:bold;">&lt;%</span>= asset.<span style="color:#9900CC;">url</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:tiny</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br /&gt;
 Small:  <span style="color:#006600; font-weight:bold;">&lt;%</span>= asset.<span style="color:#9900CC;">url</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:small</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br /&gt;
 Medium:  <span style="color:#006600; font-weight:bold;">&lt;%</span>= asset.<span style="color:#9900CC;">url</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:medium</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br /&gt;
 Large:  <span style="color:#006600; font-weight:bold;">&lt;%</span>= asset.<span style="color:#9900CC;">url</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:large</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br /&gt;
 XL:  <span style="color:#006600; font-weight:bold;">&lt;%</span>= asset.<span style="color:#9900CC;">url</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:xlarge</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br /&gt;
 XXL:  <span style="color:#006600; font-weight:bold;">&lt;%</span>= asset.<span style="color:#9900CC;">url</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:xxlarge</span><span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">%&gt;</span>&lt;br /&gt;   
 Original:  <span style="color:#006600; font-weight:bold;">&lt;%</span>= asset.<span style="color:#9900CC;">url</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
&lt;/p&gt;</pre></div></div>

<p><em><strong>Other Notes:</strong></em></p>
<p>You can use this to attach an attachment if you were going to use a different view, for example an upload view with @document.assets.attach(@asset)</p>
<p>You can nuke an attachment by either calling @document.assets.detach or @document.assets.detach(@asset) depending upon how you are going about dealing with removing attachments. @document.assets.detach  will nuke ALL attachments associated with that document, @document.essay.assets.detach(@asset) will nuke just that asset you are referencing. </p>
]]></content:encoded>
			<wfw:commentRss>http://burm.net/2008/10/17/ruby-on-rails-polymorphic-paperclip-plugin-tutorial/feed/</wfw:commentRss>
		<slash:comments>45</slash:comments>
		</item>
		<item>
		<title>The Ruby On Rails Paperclip Plugin Tutorial &#8211; Easy Image Attachments</title>
		<link>http://burm.net/2008/10/07/the-ruby-on-rails-paperclip-plugin-tutorial-easy-image-attachments/</link>
		<comments>http://burm.net/2008/10/07/the-ruby-on-rails-paperclip-plugin-tutorial-easy-image-attachments/#comments</comments>
		<pubDate>Wed, 08 Oct 2008 01:00:38 +0000</pubDate>
		<dc:creator>John Burmeister</dc:creator>
				<category><![CDATA[My Projects]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[paperclip]]></category>
		<category><![CDATA[RoR]]></category>

		<guid isPermaLink="false">http://burm.net/?p=127</guid>
		<description><![CDATA[I used Paperclip for my latest project, and I figured I would give a brief tutorial on how to use it. Paperclip &#8211; Paperclip is intended as an easy file attachment library for ActiveRecord. The intent behind it was to keep setup as easy as possible and to treat files as much like other attributes [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fburm.net%2F2008%2F10%2F07%2Fthe-ruby-on-rails-paperclip-plugin-tutorial-easy-image-attachments%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fburm.net%2F2008%2F10%2F07%2Fthe-ruby-on-rails-paperclip-plugin-tutorial-easy-image-attachments%2F&amp;source=burmjohn&amp;style=compact&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I used Paperclip for my latest project, and I figured I would give a brief tutorial on how to use it.</p>
<p><strong>Paperclip</strong> &#8211; Paperclip is intended as an easy file attachment library for ActiveRecord. The intent behind it was to keep setup as easy as possible and to treat files as much like other attributes as possible. <a href="http://github.com/thoughtbot/paperclip/tree/master">http://github.com/thoughtbot/paperclip/tree/master</a></p>
<p>In my case, I wanted to allow an attachment to an Event, which will have one photo.</p>
<p>To install:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">script<span style="color:#006600; font-weight:bold;">/</span>plugin install git:<span style="color:#006600; font-weight:bold;">//</span>github.<span style="color:#9900CC;">com</span><span style="color:#006600; font-weight:bold;">/</span>thoughtbot<span style="color:#006600; font-weight:bold;">/</span>paperclip.<span style="color:#9900CC;">git</span></pre></div></div>

<p>Create your migration, again in my case I was adding the images to my Events model / DB, so I did the following:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">script<span style="color:#006600; font-weight:bold;">/</span>generate migration AddPhotosToEvents</pre></div></div>

<p>Open up your newly created migration with your favorite Text editor, and add the following:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> AddPhotoToEvent <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#CC00FF; font-weight:bold;"><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span></span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
    add_column <span style="color:#ff3333; font-weight:bold;">:events</span>, <span style="color:#ff3333; font-weight:bold;">:photo_file_name</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>
    add_column <span style="color:#ff3333; font-weight:bold;">:events</span>, <span style="color:#ff3333; font-weight:bold;">:photo_content_type</span>, <span style="color:#ff3333; font-weight:bold;">:string</span>
    add_column <span style="color:#ff3333; font-weight:bold;">:events</span>, <span style="color:#ff3333; font-weight:bold;">:photo_file_size</span>, <span style="color:#ff3333; font-weight:bold;">:integer</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>
    remove_column <span style="color:#ff3333; font-weight:bold;">:events</span>, <span style="color:#ff3333; font-weight:bold;">:photo_file_name</span>
    remove_column <span style="color:#ff3333; font-weight:bold;">:events</span>, <span style="color:#ff3333; font-weight:bold;">:photo_content_type</span>
    remove_column <span style="color:#ff3333; font-weight:bold;">:events</span>, <span style="color:#ff3333; font-weight:bold;">:photo_file_size</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Then rake your migration so the new columns are added to your database:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;">rake db:migrate</pre></div></div>

<p>Next you need to tell your model to use Paperclip, again I am using the Event model as an example, the #Paperclip and below is what you need to add.  If you notice below I added 4 options to the :styles.  I wanted to have a few different sizes generated when a image was uploaded, i named them appropriately (you can name them whatever you wish).   Please note when you put a # on the end it signifies that you want that <strong>exact</strong> aspect ratio, it will crop your photo automatically.  When you use > on the end it will make the largest side the size you specify and keep the aspect ratio uploaded.  In addition note that because we specified has_attached_file :photo its going to look for that naming convention we created in the migration above.  In addition it uses that name to store your photo in the public folder of your application.  So our photo url is going to be as follows: /public/photos/(event#)/(size_name)/image_name</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Event <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#CC00FF; font-weight:bold;"><span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span></span>
<span style="color:#5A0A0A; font-weight:bold;">belongs_to</span> <span style="color:#ff3333; font-weight:bold;">:user</span>
<span style="color:#5A0A0A; font-weight:bold;">validates_presence_of</span> <span style="color:#ff3333; font-weight:bold;">:title</span>, <span style="color:#ff3333; font-weight:bold;">:on</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:create</span>, <span style="color:#ff3333; font-weight:bold;">:message</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;can't be blank&quot;</span>
<span style="color:#5A0A0A; font-weight:bold;">validates_presence_of</span> <span style="color:#ff3333; font-weight:bold;">:teaser</span>, <span style="color:#ff3333; font-weight:bold;">:on</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:create</span>, <span style="color:#ff3333; font-weight:bold;">:message</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;can't be blank&quot;</span>
<span style="color:#5A0A0A; font-weight:bold;">validates_presence_of</span> <span style="color:#ff3333; font-weight:bold;">:subject</span>, <span style="color:#ff3333; font-weight:bold;">:on</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#ff3333; font-weight:bold;">:create</span>, <span style="color:#ff3333; font-weight:bold;">:message</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;can't be blank&quot;</span>
<span style="color:#008000; font-style:italic;"># Paperclip</span>
has_attached_file <span style="color:#ff3333; font-weight:bold;">:photo</span>,
  <span style="color:#ff3333; font-weight:bold;">:styles</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span>
    <span style="color:#ff3333; font-weight:bold;">:thumb</span><span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;100x100#&quot;</span>,
    <span style="color:#ff3333; font-weight:bold;">:small</span>  <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;150x150&gt;&quot;</span>,
    <span style="color:#ff3333; font-weight:bold;">:medium</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;300x300&gt;&quot;</span>,
    <span style="color:#ff3333; font-weight:bold;">:large</span> <span style="color:#006600; font-weight:bold;">=&gt;</span>   <span style="color:#996600;">&quot;400x400&gt;&quot;</span> <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>Next you need to make sure you put the html => { :multipart => true } in <strong>both </strong>your edit and new views for the model you are working with.  Example in my case:</p>

<div class="wp_syntax"><div class="code"><pre class="rails" style="font-family:monospace;"><span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#5A0A0A; font-weight:bold;">form_for</span><span style="color:#006600; font-weight:bold;">&#40;</span>@event,:html <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:multipart</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#006600; font-weight:bold;">&#125;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>f<span style="color:#006600; font-weight:bold;">|</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= f.<span style="color:#9900CC;">error_messages</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
  <span style="color:#006600; font-weight:bold;">&lt;%</span>= <span style="color:#5A0A0A; font-weight:bold;">render</span> <span style="color:#ff3333; font-weight:bold;">:partial</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">'form'</span>, <span style="color:#ff3333; font-weight:bold;">:locals</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#ff3333; font-weight:bold;">:f</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> f <span style="color:#006600; font-weight:bold;">&#125;</span> <span style="color:#006600; font-weight:bold;">%&gt;</span>
<span style="color:#006600; font-weight:bold;">&lt;%</span> <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">%&gt;</span></pre></div></div>

<p>You then need to add the file_field to your new and edit forms or your _form partial like in my case:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;p&gt;
  &lt;%= f.label 'Photo' %&gt;&lt;br /&gt;
  &lt;%= f.file_field :photo %&gt;
&lt;/p&gt;</pre></div></div>

<p>Next up is deciding on how you are going to use / view your images.   In my case I wanted to show a few different sizes in the Event view.  I also wanted to make sure I am only going to show photo&#8217;s if one exists. In this example I am just showing the small and medium sizes we generated:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;% if @event.photo.exists? then %&gt;
&lt;p&gt;Small:&lt;%= image_tag @event.photo.url(:small) %&gt;&lt;/p&gt;
&lt;p&gt;Medium:&lt;%= image_tag @event.photo.url(:medium) %&gt;&lt;/p&gt;
&lt;% else %&gt;
&lt;p&gt; There are no photo's attached, upload one. &lt;/p&gt;
&lt;% end %&gt;</pre></div></div>

<h3>A few other notes&#8230;</h3>
<p>
Calling @event.photo.nil destroys the photo</p>
<p>Also checkout this great tutorial <a href="http://jimneath.org/2008/04/17/paperclip-attaching-files-in-rails/" target="_blank">Jim put up here.</a></p>
<p><a href="http://jimneath.org/2008/04/17/paperclip-attaching-files-in-rails/" target="_blank"></a><br />
Thats all for now, I&#8217;ll try to post an update with some more options / features when using the Paperclip plugin in the future.</p>
<p>*** Checkout my other tutorial on <a href="http://burm.net/2008/10/17/ruby-on-rails-polymorphic-paperclip-plugin-tutorial/">polymorphic paperclip</a> if you would like to have multiple image attachments.</p>
]]></content:encoded>
			<wfw:commentRss>http://burm.net/2008/10/07/the-ruby-on-rails-paperclip-plugin-tutorial-easy-image-attachments/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
	</channel>
</rss>

