This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Tentarei explicar o uso de cada funcionalidade. | |
class User < ActiveRecord::Base | |
# código | |
# paperclip upload | |
has_attached_file :avatar, | |
# O style diz o tamanho das imagens que será geradas. | |
# A função > diz o tamanho da imagem que ela deverá ficar. Caso queira a função crop basta trocar > por # | |
# O :png diz que qualquer upload de imagem será gerada uma imagem png para uso. | |
# Posso dar nome aos tamanhos das imagens que vou querer gerar no meu sistema. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Migration para adicionar as colunas do paperclip no model. | |
class AddAvatarColumnsToUser < 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Nome do arquivo. | |
avatar_file_name, do tipo string | |
# Extensão do arquivo. | |
avatar_content_type, do tipo string | |
# Tamanho do arquivo. | |
avatar_file_size, do tipo integer | |
# Data de upload do arquivo. | |
avatar_updated_at, do tipo datetime |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Caminho da pasta onde descompactar o download do plugin. | |
# Faça download do plugin em: | |
# url: http://github.com/thoughtbot/paperclip/tree/master | |
$ SUA_APLICAÇÃO/vendor/plugins/ | |
# Se for baixar via terminal utilizando a estrutura do Rails. | |
$ script/plugin install git://github.com/thoughtbot/paperclip.git |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- partial --> | |
<%= f.error_messages %> | |
<p> | |
<%= f.label :ordinary_name %><br /> | |
<%= f.text_field :ordinary_name %> | |
</p> | |
<p> | |
<%= f.label :scientific_name %><br /> | |
<%= f.text_field :scientific_name %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Bem, irei falar sobre o plugin paperclip para upload de arquivo, mas aqui irei da ênfase ao upload de imagens. | |
Vamos iniciar o nosso tutorial? | |
O que iremos precisar? | |
Ter o Rails instalado, e ter o imagemagick, caso não tenha ambos na sua maquina instale. Mas porque o imagemagick? o plugin paperclip utiliza o programa imagemagick para fazer modificações/trabalhar com as imagens que você vai submeter. | |
O paperclip por padrão ele requerer 4 campos/colunas no model. São eles: | |
- image_file_name | |
- image_content_type | |
- image_file_size | |
- image_updated_at |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- /views/confirmation/thanks.html.erb --> | |
Olá <%= @user.name %>, | |
Sua conta foi criada com sucesso. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /controller/users_controller.rb | |
class UsersController < ApplicationController | |
#código | |
def create | |
@user = User.new(params[:user]) | |
respond_to do |format| | |
if @user.save | |
flash[:notice] = "User #{@user.name} was successfully created." | |
# Basta incluir esta unica linha. | |
Confirmation.deliver_confirmation_for_email(@user) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /app/models/confirmation.rb | |
class Confirmation < ActionMailer::Base | |
def confirmation_for_email(user) | |
# irá utilizar o email digitado no cadastro do usuário, onde será enviar o mail de confirmação | |
recipients user.email | |
from "Administração Sistema XYZ <[email protected]>" | |
subject "Confirmação de Criação de Conta" | |
sent_on Time.now # hora de envio do email | |
body :user => user # objeto que será passado para o helper do template | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ruby script/generate mailer confirmation |