1 - Instalando
Segue procedimento para instalar paperclip no macosx
git clone git://github.com/thoughtbot/paperclip.git
Fazendo o passenger achar o ImageMagick, crie um arquivo paperclip.rb em /config/initializers/ com o conteúdo
Paperclip.options[:image_magick_path] = '/opt/local/bin/' if RAILS_ENV == "development"
2 - Como usar o Paperclip?
Digamos que precise colocar uma foto em meu sistema de noticias, fazemos da seguinte forma:
Coloque no model:
class Noticia < ActiveRecord::Base
has_attached_file :noticia, :styles => { :original => "300x300>", :thumb => "100x100>" }
end
Na migration:
class AddNoticiaColumnsToUser < ActiveRecord::Migration
def self.up
add_column :users, :noticia_file_name, :string
add_column :users, :noticia_content_type, :string
add_column :users, :noticia_file_size, :integer
add_column :users, :noticia_updated_at, :datetime
end
def self.down
remove_column :users, :noticia_file_name
remove_column :users, :noticia_content_type
remove_column :users, :noticia_file_size
remove_column :users, :noticia_updated_at
end
end
No _form da view:
<% form_for(@noticia, :html => {:multipart => true, :class => 'form1' }) do |f| field_set_tag( "Dados do noticia" ) do %>
<%= form.file_field :noticia %>
<% end %>
No controller(caso não esteja):
E, finalmente, na view:
<%= image_tag @noticia.picture.url(:original), :class => 'index_noticias_img' unless @noticia.picture_file_name.blank? %>
Para maiores informações acesse
aqui.