<?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; file upload</title>
	<atom:link href="http://reinventar.com/tag/file-upload/feed/" rel="self" type="application/rss+xml" />
	<link>http://reinventar.com</link>
	<description>now with double the colour and flavor!</description>
	<lastBuildDate>Wed, 28 Jul 2010 14:52:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Upload files using Paperclip on Ruby on Rails</title>
		<link>http://reinventar.com/2009/03/upload-files-with-paperclip-on-ruby-on-rails/</link>
		<comments>http://reinventar.com/2009/03/upload-files-with-paperclip-on-ruby-on-rails/#comments</comments>
		<pubDate>Thu, 19 Mar 2009 10:25:15 +0000</pubDate>
		<dc:creator>psousa</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><code>script/plugin install git://github.com/thoughtbot/paperclip.git</code></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><code>class AddAttachmentsAvatarToUser &lt; ActiveRecord::Migration
  def self.up
    add_column :users, :avatar_file_name, :string
    add_column :users, :avatar_content_type, :string
    add_column :users, :avatar_file_size, :integer
    add_column :users, :avatar_updated_at, :datetime
  end

  def self.down
    remove_column :users, :avatar_updated_at
    remove_column :users, :avatar_file_name
    remove_column :users, :avatar_content_type
    remove_column :users, :avatar_file_size
  end
end</code></pre>
<p>Then we define the attribute in the model:</p>
<pre><code>
class User &lt; ActiveRecord::Base
has_attached_file :avatar, :styles =&gt; { :large =&gt; "120x120&gt;", :medium =&gt; "48x48&gt;", :thumb =&gt; "26x26&gt;" }
end
</code></pre>
<p>Here&#8217;s an example on how your form should be:</p>
<pre><code>&lt;% form_for :user, @user, :url =&gt; user_path, :html =&gt; { :multipart =&gt; true } do |form| %&gt;
    &lt;%= form.file_field :avatar %&gt;
  &lt;% end %&gt;</code></pre>
<p>Your controller doesn&#8217;t have to do anything specific to handle the avatar, just save the user as usual.</p>
<pre><code>  def create
    @user = User.create( params[:user] )
  end</code></pre>
<p>And finally, if you want to show the avatar in your view, here&#8217;s some examples:</p>
<pre><code>  &lt;%= image_tag @user.avatar.url %&gt;
  &lt;%= image_tag @user.avatar.url(:medium) %&gt;
  &lt;%= image_tag @user.avatar.url(:thumb) %&gt;</code></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. :)</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 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>
]]></content:encoded>
			<wfw:commentRss>http://reinventar.com/2009/03/upload-files-with-paperclip-on-ruby-on-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
