Skip to content

Paperclip on Rails 3 — damn easy!

by dkberktas on September 12th, 2010

Paperclip is a Rails plug-in for attaching files to models. Using Paperclip is also very easy to use, it can thumbnail your images and also send the files to Amazon S3.

From this link, you can find the steps for creating a Rails project from scratch with paperclip.
http://jameswilding.net/2010/07/24/paperclip-rails-3/

As I have already had a project that needs file attachments, I follow a slightly different path.

The steps for adding a file attachment is as follows:

  1. Install Paperclip
  2. $ rails plugin install git://github.com/thoughtbot/paperclip.git
  3. Add image attribute to your model
  4.   #paperclip
      has_attached_file :photo,
         :styles => {
           :thumb=> "100x100#",
           :small  => "400x400>" }
  5. Create migration file and add the necessary columns to your database
  6. $ rails generate migration add_photo_to_microposts photo_file_name:string photo_content_type:string photo_file_size:integer
    rake db:migrate
  7. Change you form
  8. <%= form_for @micropost  , :html => { :multipart => true } do |f| %>
    <%= f.file_field :photo %>
    ...
  9. Change view that will show the uploaded image (optional)
  10. <% if micropost.photo.exists? then %>
    	<%= image_tag micropost.photo.url(:small) %>
    <% end %>

That’s it. It is done.

From → english, tech

5 Comments
  1. Amina permalink

    Hello, I followed this tutorial but I have a problem. When i start the server, I get a message:

    “NoMethodError in UsersController#new

    undefined method `has_attached_file’ for #”

    What do you think the problem is? Tnx in advance :D

  2. DevBrPb permalink

    Amina, this problem can solved if you include the paperclip in you gemfile

    # Paperclip
    gem “paperclip”, :git => “http://github.com/thoughtbot/paperclip.git”

  3. Wow, thank you for this. You made this very easy. :D

  4. Yes thanks man, the live goes on :D very nice work

  5. Vladyslav permalink

    Hello.
    I have the same problem as Amnia, but gem “paperclip”, :git => “http://github.com/thoughtbot/paperclip.git” not helps. it returns that: The filename, directory name, or volume label syntax is incorrect.
    Does someone know correct syntax.
    (using ruby 1.93 and rails 3.2.3)
    Thank you

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS