Professional Documents
Culture Documents
#Instalar requisitos
$ sudo apt-get install imagemagick --fix-missing
# En Gemfile
gem "paperclip", "~> 4.2"
PAPERCLIP | AGREGAR A MODELO
# Agregar avatar a User
$ rails g paperclip user avatar
# Migración
class AddAvatarColumnsToUsers < ActiveRecord::Migration
def up
add_attachment :users, :avatar
# Adiciona las columnas:
# avatar_file_name
# avatar_file_size
# avatar_content_type
# avatar_updated_at
end
def down
remove_attachment :users, :avatar
end
end
PAPERCLIP | CONFIGURAR EN MODELO
class User < ActiveRecord::Base
has_attached_file :avatar,
:styles => { :medium => "300x300>",
:thumb => "100x100>" },
:default_url => "/images/:style/missing.png"
end
PAPERCLIP | VALIDAR EN MODELO
class User < ActiveRecord::Base
has_attached_file :avatar,
:styles => { :medium => "300x300>",
:thumb => "100x100>" },
:default_url => "/images/:style/missing.png"
# Validate presence
validates :avatar, :attachment_presence => true
end
PAPERCLIP | VALIDAR EN MODELO
class User < ActiveRecord::Base
has_attached_file :avatar,
styles: {medium: "300x300>",
thumb: "100x100>" },
default_url: "/images/:style/missing.png"
# Validate presence
validates :avatar, attachment_presence: true, presence: true
# Validate two or more conditions
validates_attachment :avatar,
presence: true,
content_type:
{content_type:
["image/jpeg", "image/gif", "image/png"]
},
size: { in: 0..10.kilobytes }
end
PAPERCLIP | NO SÓLO IMÁGENES
class Product < ActiveRecord::Base
has_attached_file :guide,
styles: {thumbnail: "60x60#"}
validates :guide, presence: true
validates_attachment :guide,
presence: true,
content_type:
{content_type: "application/pdf" }
end
PAPERCLIP | REGISTRO
<%= simple_form_for @user, html: {multipart: true} do |f| %>
<%= f.input :avatar, as: :file %>
<% end %>
PAPERCLIP | VISUALIZACIÓN
<%= image_tag @user.avatar.url(:medium) %>
# ó
$ bundle
$ rake acts_as_taggable_on_engine:install:migrations
$ rake db:migrate
$ rake acts_as_taggable_on_engine:tag_names:collate_bin
ACT_AS_TAGGABLE_ON | MODELO
class Product < ActiveRecord::Base
act_as_taggable # act_as_tagable_on :tags
end