Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Quick Start

sadfuzzy edited this page Sep 18, 2012 · 2 revisions

This page gives you a quick overview of what you need to do to start using Paperclip. You can also check out this swanky demo app, too.

In your model:

  class User < ActiveRecord::Base
    attr_accessible :avatar
    has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" }
  end

In your migrations:

  class AddAvatarColumnsToUsers < ActiveRecord::Migration
    def self.up
      add_attachment :users, :avatar
    end

    def self.down
      remove_attachment :users, :avatar
    end
  end

(Or you can use migration generator: rails generate paperclip user avatar)

In your edit and new views:

  <%= form_for @user, :url => users_path, :html => { :multipart => true } do |form| %>
    <%= form.file_field :avatar %>
  <% end %>

In your controller:

  def create
    @user = User.create( params[:user] )
  end

In your show view:

  <%= image_tag @user.avatar.url %>
  <%= image_tag @user.avatar.url(:medium) %>
  <%= image_tag @user.avatar.url(:thumb) %>

To detach a file, simply set the attribute to nil:

  @user.avatar = nil
  @user.save