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







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
Amina, this problem can solved if you include the paperclip in you gemfile
# Paperclip
gem “paperclip”, :git => “http://github.com/thoughtbot/paperclip.git”
Wow, thank you for this. You made this very easy.
Yes thanks man, the live goes on
very nice work
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