Ruby On Rails Polymorphic Paperclip Plugin Tutorial

Posted on the October 17th, 2008 under My Projects,programming,Ruby on Rails by John

This tutorial is an extension of the paperclip tutorial I put up last week. This time we are going to take advantage of polymorphic paperclip. Polymorphic paperclip utilizes separate tables (an assets table and an attachings table) to track your attachments. This allows for an unlimited number of attachments per item per model. In the first tutorial I showed you how to attach just one item, although you could add more columns to your table to handle additional attachments this way is a lot more flexible. While this plugin is not perfect, there is a lot of room for improvement, its a great starting point. Perhaps a fork is on its way from me…

Project Page: http://github.com/heavysixer/paperclippolymorph/tree/master

First things first, lets install the plugin:

script/plugin git://github.com/heavysixer/paperclippolymorph.git

Lets setup our new migration that is needed. After reading the rdoc, I noticed there was a new generator installed. Great, this makes things even easier. Lets verify that the generator is available for use, first run:

script/generate

This should produce a list of all generators available for use, my list below might vary from yours depending upon what plugins you have installed…

Installed Generators
  Plugins (vendor/plugins): authenticated, forgot_password, open_id_authentication_tables, paperclip, polymorphic_paperclip, roles, rspec, rspec_controller, rspec_model, rspec_scaffold, upgrade_open_id_authentication_tables
  Builtin: controller, integration_test, mailer, migration, model, observer, plugin, resource, scaffold, session_migration

As you will notice there is a polymorphic_paperclip generator, go ahead an run it as follows:

script/generate polymorphic_paperclip

Before you go and run rake db:migrate, open up the migration and modify the assets_count to attachings_count – it seems there is a minor bug. I notified the author of the plugin and will submit a patch via git. Now its OK to run rake db:migrate

Now we have the migration generated and the tables have been added to your database. Next we have to add (or change if you already have paperclip setup) the model where you want to have attachments by adding acts_as_polymorphic_paperclip. As an example, I posted my documents model below.

class Document < ActiveRecord::Base
# Document Belongs To A User
belongs_to :user
# for paperclip (polymorphic)
acts_as_polymorphic_paperclip 
# Validations
...

Note: Ideally I would like to be able to override the styles settings that are set in the plugins assets.rb, however I went ahead and hard coded them there to fit my needs. Example:

class Asset < ActiveRecord::Base
  has_many :attachings, :dependent => :destroy
  has_attached_file :data,
                    :styles => {
                    :thumb=> "100x100#",
                    :small  => "150x150>",
                    :medium => "300x300>",
                    :large =>   "500x500>",
                    :xlarge =>   "600x600>",
                    :xxlarge =>   "800x800>" }

For now, lets move onto the views where you are going to allow attachments. We are just going to have one upload, perhaps in another tutorial we will look at handling multiple uploads.

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(@document,:html => { :multipart => true }) do |f| %>
  <%= f.error_messages %>
  <%= render :partial => 'form', :locals => { :f => f } %>
<% end %>

Next you need to add the file upload field to your _form or edit/new views.

<p>
  Attach a file or image <br />
  <%= f.file_field :data%>
</p>

Now lets go ahead and make these attachments viewable in the document. For the edit view I added the following:

<p><%= image_tag asset.url(:medium) %></p>
<p>Tiny:  <%= asset.url(:tiny) %><br />
 Small:  <%= asset.url(:small)%><br />
 Medium:  <%= asset.url(:medium)%><br />
 Large:  <%= asset.url(:large)%><br />
 XL:  <%= asset.url(:xlarge)%><br />
 XXL:  <%= asset.url(:xxlarge)%><br />   
 Original:  <%= asset.url %>
</p>

Other Notes:

You can use this to attach an attachment if you were going to use a different view, for example an upload view with @document.assets.attach(@asset)

You can nuke an attachment by either calling @document.assets.detach or @document.assets.detach(@asset) depending upon how you are going about dealing with removing attachments. @document.assets.detach will nuke ALL attachments associated with that document, @document.essay.assets.detach(@asset) will nuke just that asset you are referencing.

Similar Posts:

Be Sociable, Share!

45 Responses to 'Ruby On Rails Polymorphic Paperclip Plugin Tutorial'

  1. October 22, 2008 at 11:38 pm
    rohandey
  2. October 27, 2008 at 11:10 am
    heavysixer
  3. November 1, 2008 at 12:54 pm
    Tom
  4. November 20, 2008 at 6:53 pm
    ryan
  5. November 20, 2008 at 9:00 pm
    John Burmeister
  6. November 20, 2008 at 10:44 pm
    EH
  7. November 27, 2008 at 7:42 am
    ron
  8. December 3, 2008 at 12:21 am
    EH
  9. December 17, 2008 at 12:26 am
    Will Merrell
  10. January 3, 2009 at 1:20 am
    EH
  11. February 11, 2009 at 10:05 pm
    Pablo Targa
  12. February 14, 2009 at 8:28 pm
    Pablo Targa
  13. February 26, 2009 at 9:55 pm
    EH
  14. March 12, 2009 at 7:42 am
    thaniyarasu
  15. March 19, 2009 at 12:49 am
    EH
  16. April 4, 2009 at 5:46 am
    Joe
  17. April 4, 2009 at 5:55 am
    Joe
  18. April 4, 2009 at 5:56 am
    Joe
  19. April 4, 2009 at 5:57 am
    Joe
  20. April 6, 2009 at 10:18 pm
    jeff
  21. April 9, 2009 at 2:07 am
    Nemo
  22. April 13, 2009 at 1:04 pm
    dimir
  23. April 17, 2009 at 1:54 pm
    Dan
  24. April 17, 2009 at 1:57 pm
    John Burmeister
  25. April 17, 2009 at 2:26 pm
    Dan
  26. April 17, 2009 at 2:35 pm
    Dan
  27. May 26, 2009 at 7:25 am
    LeeRichmond
  28. May 28, 2009 at 11:58 am
    Andreas Ludewig
  29. May 28, 2009 at 12:02 pm
    Andreas Ludewig
  30. August 25, 2009 at 6:19 am
    Kristian Mandrup
  31. August 25, 2009 at 8:59 am
    Kristian Mandrup
  32. November 4, 2009 at 10:55 pm
    Antoine
  33. November 13, 2009 at 2:06 am
    adam
  34. November 13, 2009 at 2:08 am
    adam
  35. November 19, 2009 at 9:14 pm
    Jayme
  36. January 23, 2010 at 8:53 pm
    rtacconi
  37. May 14, 2010 at 1:16 pm
    Mihai
  38. August 27, 2010 at 12:45 am
    Levi Rosol
  39. September 8, 2010 at 6:01 pm
    avery
  40. September 8, 2010 at 6:03 pm
    avery
  41. July 18, 2011 at 2:23 am
    ashis

Leave a Reply




XHTML::
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>