Precisando fazer upload de imagens no rails? Eu usei o plugin Paperclip. Segue um resumão passo a passo (?) de como utilizar o plugin.
1. Para utilizar o PaperClip é necessário instalar o ImageMagick:
http://www.imagemagick.org/script/binary-releases.php
2. Reiniciar o PC
3. Instalar o PaperClip:
http://github.com/thoughtbot/paperclip/tree/master
4. Criar migrations que incluam os campos necessários:
class AddUpload < ActiveRecord::Migration
def self.up
change_table :tabela do |t|
t.string :imagem_file_name
t.string :imagem_content_type
t.integer :imagem_file_size
t.datetime :imagem_updated_at
end
end
def self.down
change_table :perguntas do |t|
t.remove :imagem_file_name, :imagem_content_type,
:imagem_file_size, :imagem_updated_at
end
change_table :respostas do |r|
r.remove :imagem_file_name, :imagem_content_type,
:imagem_file_size, :imagem_updated_at
end
end
end
5. Rodar rake db:migrate
6. Incluir nos modelos envolvidos:
has_attached_file :imagem, :styles => {
:medium => "300x300>",
:thumb => "100x100>" }
7. Colocar nos formulários envolvidos :html => {:multipart => true}
<% form_for(@pergunta, :html => {:multipart => true }) do |f| %>
8. O campo para upload:
<p>
<%= f.label :imagem %><br />
<%= f.file_field :imagem %>
</p>
9. Exibir a imagem:
<%= image_tag @pergunta.imagem.url %>
<%= image_tag @pergunta.imagem.url(:medium) %>
<%= image_tag @pergunta.imagem.url(:thumb) %>
10. Boas referências:
http://forum.rubyonbr.org/forums/5/topics/3736
http://www.nomedojogo.com/2008/11/12/por-que-paperclip/