05
Jul 09

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/

publicado por dessa às 00:42

Julho 2009
Dom
Seg
Ter
Qua
Qui
Sex
Sab

1
2
3
4

5
6
7
8
9
10
11

12
13
14
15
16
17
18

19
20
21
22
23
24
25

26
27
28
29
30
31


subscrever feeds
pesquisar
 
Subscrever por e-mail

A subscrição é anónima e gera, no máximo, um e-mail por dia.

blogs SAPO