#134 Paperclip
Nov 03, 2008 | 7 minutes | Plugins
Need to add image attachments to a model? See how with paperclip in this episode.
- Download:
- source codeProject Files in Zip (150 KB)
- mp4Full Size H.264 Video (11.5 MB)
- m4vSmaller H.264 Video (7.81 MB)
- webmFull Size VP8 Video (20.3 MB)
- ogvFull Size Theora Video (15.3 MB)
Resources
- Paperclip
- Paperclip on GitHub
- Paperclip RDocs
- Paperclip Google Group
- Paperclip Tutorial
- Another (big) Paperclip Tutorial
- Determining Image Sizes
- Full Episode Source Code
bash
script/plugin install git://github.com/thoughtbot/paperclip.git
script/generate paperclip product photo
rake db:migrate
script/plugin install git://github.com/thoughtbot/paperclip.git script/generate paperclip product photo rake db:migrate
models/product.rb
has_attached_file :photo, :styles => { :small => "150x150>" },
:url => "/assets/products/:id/:style/:basename.:extension",
:path => ":rails_root/public/assets/products/:id/:style/:basename.:extension"
validates_attachment_presence :photo
validates_attachment_size :photo, :less_than => 5.megabytes
validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']
has_attached_file :photo, :styles => { :small => "150x150>" }, :url => "/assets/products/:id/:style/:basename.:extension", :path => ":rails_root/public/assets/products/:id/:style/:basename.:extension" validates_attachment_presence :photo validates_attachment_size :photo, :less_than => 5.megabytes validates_attachment_content_type :photo, :content_type => ['image/jpeg', 'image/png']
products/show.html.erb
<%= image_tag @product.photo.url(:small) %>
<%= image_tag @product.photo.url(:small) %>
products/_form.html.erb
<% form_for @product, :html => { :multipart => true } do |f| %>
...
<%= f.file_field :photo %>
...
<% end %>
<% form_for @product, :html => { :multipart => true } do |f| %> ... <%= f.file_field :photo %> ... <% end %>

