Tuesday, June 24, 2008

PaperClip and Rails 2.1

These are a couple of quick tips for using PaperClip 2.1.2 and Rails 2.1. I don't know if there is a difference with other version combinations.

For security reasons, you may want to store your attachment out of the public directory:
class DocumentVersion < ActiveRecord::Base
belongs_to :document
has_attached_file :version,
:path => ":rails_root/documents/:class/:id/:style/:basename.:extension"
...
end

To access this:
send_file(@document_version.version.path)


I found the directory defaults a little odd, so if you want to specify it:
class Picture < ActiveRecord::Base
has_attached_file :image,
:path => ":rails_root/public/images/pictures/:class/:id/:style/:basename.:extension",
:url => "/images/pictures/:class/:id/:style/:basename.:extension"
end


Note the :url parameter. If you set the path, you must set the :url accordingly. Of course, this only matters if you are going to call the url method.

0 comments: