0 Think Different, Rails Edition
Cruising the web I’m finding a lot of changes in Rails “standards”. Developers are writing better code and just doing things better in general.
File Attachments
Attachment_fu has been the standard for a very long time. I mean it is the successor to acts_as_attachment the first production-stable file attachment scheme for rails. But lately there is a new contender. Welcome to the ring, Paperclip by Jon Yurek of Thoughbot.
I personally have not given it a test run, but skin deep, it looks impressive.
class User < ActiveRecord::Base
# Paperclip
has_attached_file :photo,
:styles => {
:thumb=> "100x100#",
:small => "150x150>" }
end
No extra models! This is great when you just need a few thumbnails for your social app (as if we don’t have enough social apps out there already). What’s beautiful and as mentioned before, you don’t need to have an extra attachment model. Can anyone spell D.R.Y around here? Jon Yurek can! Let’s look at some view code.
# What? Is that a thumbnail without using an extra model? Yes sir!
<%= image_tag @user.photo.url %>
<%= image_tag @user.photo.url(:thumb) %>
Attachment_fu makes you go the full 100 yards when as you access your file in the views. Personally, I’ve always hated that as the attribute access methods resulted in code that seemed longer than your typical img tag with all the important tag attributes. As you can, Paperclip says no to long attribute method chains. I love DRYness.
Give it go by getting it at Github here