<?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>Reinventar &#187; paperclip</title>
	<atom:link href="http://reinventar.com/tag/paperclip/feed/" rel="self" type="application/rss+xml" />
	<link>http://reinventar.com</link>
	<description></description>
	<lastBuildDate>Tue, 05 Apr 2011 13:25:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Upload files using Paperclip on Ruby on Rails</title>
		<link>http://reinventar.com/2009/03/19/upload-files-with-paperclip-on-ruby-on-rails/</link>
		<comments>http://reinventar.com/2009/03/19/upload-files-with-paperclip-on-ruby-on-rails/#comments</comments>
		<pubDate>Thu, 19 Mar 2009 10:25:15 +0000</pubDate>
		<dc:creator>Pedro Sousa</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[file upload]]></category>
		<category><![CDATA[paperclip]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://reinventar.com/?p=543</guid>
		<description><![CDATA[Paperclip is an awesome plugin to handle file uploads in Ruby on Rails. I&#8217;ve been using in a couple of projects lately and I thought I&#8217;d talk a bit about it. First of all, you need to install the plugin on your project by doing: script/plugin install git://github.com/thoughtbot/paperclip.git from your project main directory. After this, [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://thoughtbot.com/projects/paperclip">Paperclip</a> is an awesome plugin to handle file uploads in Ruby on Rails. I&#8217;ve been using in a couple of projects lately and I thought I&#8217;d talk a bit about it.</p>
<p>First of all, you need to install the plugin on your project by doing:</p>
<pre>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">script/plugin install git://github.com/thoughtbot/paperclip.git</div></div>
</pre>
<p>from your project main directory. After this, usage is very simple.</p>
<p>Let&#8217;s imagine I have a user model and I want to have a avatar attribute to include a picture of the user and let&#8217;s say we want that avatar to be resized to 3 different sizes (120&#215;120, 48&#215;48 and 26&#215;26). Paperclip does this using ImageMagick.</p>
<p>We&#8217;re first going to create a migration to hold all this information:</p>
<pre>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">class AddAttachmentsAvatarToUser &amp;lt; ActiveRecord::Migration<br />
&nbsp; def self.up<br />
&nbsp; &nbsp; add_column :users, :avatar_file_name, :string<br />
&nbsp; &nbsp; add_column :users, :avatar_content_type, :string<br />
&nbsp; &nbsp; add_column :users, :avatar_file_size, :integer<br />
&nbsp; &nbsp; add_column :users, :avatar_updated_at, :datetime<br />
&nbsp; end<br />
<br />
&nbsp; def self.down<br />
&nbsp; &nbsp; remove_column :users, :avatar_updated_at<br />
&nbsp; &nbsp; remove_column :users, :avatar_file_name<br />
&nbsp; &nbsp; remove_column :users, :avatar_content_type<br />
&nbsp; &nbsp; remove_column :users, :avatar_file_size<br />
&nbsp; end<br />
end</div></div>
</pre>
<p>Then we define the attribute in the model:</p>
<pre>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">class User &amp;lt; ActiveRecord::Base<br />
has_attached_file :avatar, :styles =&amp;gt; { :large =&amp;gt; &quot;120x120&amp;gt;&quot;, :medium =&amp;gt; &quot;48x48&amp;gt;&quot;, :thumb =&amp;gt; &quot;26x26&amp;gt;&quot; }<br />
end</div></div>
</pre>
<p>Here&#8217;s an example on how your form should be:</p>
<pre>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&amp;lt;% form_for :user, @user, :url =&amp;gt; user_path, :html =&amp;gt; { :multipart =&amp;gt; true } do |form| %&amp;gt;<br />
&nbsp; &nbsp; &amp;lt;%= form.file_field :avatar %&amp;gt;<br />
&nbsp; &amp;lt;% end %&amp;gt;</div></div>
</pre>
<p>Your controller doesn&#8217;t have to do anything specific to handle the avatar, just save the user as usual.</p>
<pre>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; def create<br />
&nbsp; &nbsp; @user = User.create( params[:user] )<br />
&nbsp; end</div></div>
</pre>
<p>And finally, if you want to show the avatar in your view, here&#8217;s some examples:</p>
<pre>
<div class="codecolorer-container text blackboard" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">&nbsp; &amp;lt;%= image_tag @user.avatar.url %&amp;gt;<br />
&nbsp; &amp;lt;%= image_tag @user.avatar.url(:medium) %&amp;gt;<br />
&nbsp; &amp;lt;%= image_tag @user.avatar.url(:thumb) %&amp;gt;</div></div>
</pre>
<p>That wasn&#8217;t very difficult. There&#8217;s some advance stuff you can do with <a href="http://thoughtbot.com/projects/paperclip">Paperclip</a>, like<a href="http://almosteffortless.com/2008/12/11/easy-upload-via-url-with-paperclip/"> upload via a URL</a> or upload your files to Amazon&#8217;s S3 or define post processing operations on your files.</p>
<p>So get creative and use it in your projects. <img onError="javascript: wp_404_images_fix = window.wp_404_images_fix || function(){}; wp_404_images_fix(this);"  src='http://reinventar.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Update:<a href="http://thewebfellas.com/blog/2008/11/2/goodbye-attachment_fu-hello-paperclip"> The WebFellas</a> have a very deep and interesting article about Paperclip, <a href="http://thewebfellas.com/blog/2008/11/2/goodbye-attachment_fu-hello-paperclip">check it out</a>.</p>
<p><a href="http://bookworms.sapo.pt"><img onError="javascript: wp_404_images_fix = window.wp_404_images_fix || function(){}; wp_404_images_fix(this);"  class="alignleft size-full wp-image-544" title="picture-52" src="http://reinventar.com/wp-content/uploads/2009/03/picture-52.png" alt="picture-52" width="468" height="320" /></a></p>
<div id="crp_related"><h3>Related Posts:</h3><ul><li><a href="http://reinventar.com/2007/07/27/using-file_column-in-ruby-on-rails/" rel="bookmark" class="crp_title">using file_column in Ruby on Rails</a></li><li><a href="http://reinventar.com/2011/04/05/install-imagemagick-on-ubuntu-with-delegates/" rel="bookmark" class="crp_title">Install Imagemagick on Ubuntu with delegates</a></li><li><a href="http://reinventar.com/2009/11/25/baseapp-orange-updated-v4/" rel="bookmark" class="crp_title">Baseapp-orange updated v4</a></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://reinventar.com/2009/03/19/upload-files-with-paperclip-on-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

