I used Paperclip for my latest project, and I figured I would give a brief tutorial on how to use it.
Paperclip – Paperclip is intended as an easy file attachment library for ActiveRecord. The intent behind it was to keep setup as easy as possible and to treat files as much like other attributes as possible. http://github.com/thoughtbot/paperclip/tree/master
In my case, I wanted to allow an attachment to an Event, which will have one photo.
To install:
script/plugin install git://github.com/thoughtbot/paperclip.git |
script/plugin install git://github.com/thoughtbot/paperclip.git
Create your migration, again in my case I was adding the images to my Events model / DB, so I did the following:
script/generate migration AddPhotosToEvents |
script/generate migration AddPhotosToEvents
Open up your newly created migration with your favorite Text editor, and add the following:
class AddPhotoToEvent < ActiveRecord::Migration
def self.up
add_column :events, :photo_file_name, :string
add_column :events, :photo_content_type, :string
add_column :events, :photo_file_size, :integer
end
def self.down
remove_column :events, :photo_file_name
remove_column :events, :photo_content_type
remove_column :events, :photo_file_size
end
end |
class AddPhotoToEvent < ActiveRecord::Migration
def self.up
add_column :events, :photo_file_name, :string
add_column :events, :photo_content_type, :string
add_column :events, :photo_file_size, :integer
end
def self.down
remove_column :events, :photo_file_name
remove_column :events, :photo_content_type
remove_column :events, :photo_file_size
end
end
Then rake your migration so the new columns are added to your database:
Next you need to tell your model to use Paperclip, again I am using the Event model as an example, the #Paperclip and below is what you need to add. If you notice below I added 4 options to the :styles. I wanted to have a few different sizes generated when a image was uploaded, i named them appropriately (you can name them whatever you wish). Please note when you put a # on the end it signifies that you want that exact aspect ratio, it will crop your photo automatically. When you use > on the end it will make the largest side the size you specify and keep the aspect ratio uploaded. In addition note that because we specified has_attached_file :photo its going to look for that naming convention we created in the migration above. In addition it uses that name to store your photo in the public folder of your application. So our photo url is going to be as follows: /public/photos/(event#)/(size_name)/image_name
class Event < ActiveRecord::Base
belongs_to :user
validates_presence_of :title, :on => :create, :message => "can't be blank"
validates_presence_of :teaser, :on => :create, :message => "can't be blank"
validates_presence_of :subject, :on => :create, :message => "can't be blank"
# Paperclip
has_attached_file :photo,
:styles => {
:thumb=> "100x100#",
:small => "150x150>",
:medium => "300x300>",
:large => "400x400>" } |
class Event < ActiveRecord::Base
belongs_to :user
validates_presence_of :title, :on => :create, :message => "can't be blank"
validates_presence_of :teaser, :on => :create, :message => "can't be blank"
validates_presence_of :subject, :on => :create, :message => "can't be blank"
# Paperclip
has_attached_file :photo,
:styles => {
:thumb=> "100x100#",
:small => "150x150>",
:medium => "300x300>",
:large => "400x400>" }
Next you need to make sure you put the html => { :multipart => true } in both your edit and new views for the model you are working with. Example in my case:
<% form_for(@event,:html => { :multipart => true }) do |f| %>
<%= f.error_messages %>
<%= render :partial => 'form', :locals => { :f => f } %>
<% end %> |
<% form_for(@event,:html => { :multipart => true }) do |f| %>
<%= f.error_messages %>
<%= render :partial => 'form', :locals => { :f => f } %>
<% end %>
You then need to add the file_field to your new and edit forms or your _form partial like in my case:
<p>
<%= f.label 'Photo' %><br />
<%= f.file_field :photo %>
</p> |
<p>
<%= f.label 'Photo' %><br />
<%= f.file_field :photo %>
</p>
Next up is deciding on how you are going to use / view your images. In my case I wanted to show a few different sizes in the Event view. I also wanted to make sure I am only going to show photo’s if one exists. In this example I am just showing the small and medium sizes we generated:
<% if @event.photo.exists? then %>
<p>Small:<%= image_tag @event.photo.url(:small) %></p>
<p>Medium:<%= image_tag @event.photo.url(:medium) %></p>
<% else %>
<p> There are no photo's attached, upload one. </p>
<% end %> |
<% if @event.photo.exists? then %>
<p>Small:<%= image_tag @event.photo.url(:small) %></p>
<p>Medium:<%= image_tag @event.photo.url(:medium) %></p>
<% else %>
<p> There are no photo's attached, upload one. </p>
<% end %>
A few other notes…
Calling @event.photo.nil destroys the photo
Also checkout this great tutorial Jim put up here.
Thats all for now, I’ll try to post an update with some more options / features when using the Paperclip plugin in the future.
*** Checkout my other tutorial on polymorphic paperclip if you would like to have multiple image attachments.
I am currently working on an interesting project for a customer of mine. Hopefully I’ll be able to share more of the details with you all when the project is near completion. The project involves creating events, parsing and caching RSS feeds, generating RSS feeds, as well as Emailing and SMS’ing subscribers daily events. I have been working on the project for the last 5 days, and I am about one third of the way through.
I wanted to share some great plugins for Ruby on Rails that I have come across, along with some tutorials. Check back for an updated post with more info.
Plugins:
Restful Authentication – This widely-used plugin provides a foundation for securely managing user authentication – http://github.com/technoweenie/restful-authentication/tree/master
acts_as_taggable_on_steroids – This plugin is based on acts_as_taggable by DHH but includes extrassuch as tests, smarter tag assignment, and tag cloud calculations. – http://github.com/mattetti/acts_as_taggable_on_steroids/tree/master
Paperclip – Paperclip is intended as an easy file attachment library for ActiveRecord. The intent behind it was to keep setup as easy as possible and to treat files as much like other attributes as possible. http://github.com/thoughtbot/paperclip/tree/master
Paperclip Polymorph – This plugin allows users of the Paperclip plugin to easily share attached files between multiple models. http://locusfoc.us/2008/6/29/paperclip-polymorph
acts_as_textiled – This simple plugin allows you to forget about constantly rendering Textile in your application. Instead, you can rest easy knowing the Textile fields you want to display as HTML will always be displayed as HTML (unless you tell your code otherwise). http://github.com/defunkt/acts_as_textiled/tree/master
Will_paginate – Pagination is just limiting the number of records displayed. Why should you let it get in your way while developing, then? This plugin makes magic happen. http://github.com/mislav/will_paginate/tree/master
Tutorials / Guides:
If you are new to Ruby on Rails be sure to check out the guides that are being worked on right now here: http://guides.rails.info/index.html
Railscasts – Free Ruby on Rails Screencasts – http://railscasts.com/
Learning Rails – Audio (Podcasts) and Screencasts – http://www.buildingwebapps.com/podcasts
Jim created a great list of plugins he found useful when he was building a social network in Rails. Check it out here: http://jimneath.org/2008/04/25/building-a-social-network-site-in-rails/
Wow.. This is quite an announcement from Google. They have released (beta) Google Gears, a way to allow web applications offline. I am reading through the API now, and checking out some of the sample applications. This could lead to some interesting applications, especially for those who are frequently on the go (laptops).
” One of the most frequently requested features for Google’s web applications is the ability to use them offline. Unfortunately, today’s web browsers lack some fundamental building blocks necessary to make offline web applications a reality. In other words, we found we needed to add a few new gears to the web machinery before we could get our apps to run offline. Gears is a browser extension that we hope — with time and plenty of input and collaboration from outside of Google — can make not just our applications but everyone’s applications work offline.