Multiple File uploading in Rails3 Application using “Papercilp”

Hi friends,

Lets see how to upload multiple files in a rails 3 application.

First create a new rails project and go inside of the project directory.

$ rails new fileuploading
$ cd fileuploading

Add the Gms in  following “Gemfile”

gem ‘nifty-generators’
gem ‘paperclip’

source ''

gem 'rails', '3.0.3'
gem 'nifty-generators'
gem 'paperclip'

# Bundle edge Rails instead:
# gem 'rails', :git => 'git://'.....

And  bundle install it.

Now we have to create a scaffold. Do a following commands.

 $ rails g nifty:scaffold Post title:string
 $ rails g nifty:layout

It asks a Permission to Overwrite the layout file. just press “y”.  It shoes like,
Overwrite /home/suresh/deal_projects/paperclip-test/app/views/layouts/application.html.erb? (enter “h” for help) [Ynaqdh]

Again migrate with db.

 $ rake db:migrate
 $ rm public/index.html
Then, Add the following line in  your /config/routes.rb , root :to => “posts#index”
Then, create a model called “Asset”
$ rails g model Asset asset_file_name:string asset_content_type:string asset_file_size:integer asset_upload_at:datetime post_id:integer
$ rake db:migrate

Now in model folder, there is a file called “asset.rb”. Add the following lines into this file.

class Asset < ActiveRecord::Base
belongs_to  :post
has_attached_file  :asset, :styles => {  :large => "640*480",  :medium => "300*300>",  :thumb => "100*100>" }


And add the following lines into “posts.rb” file

class Post < ActiveRecord::Base
attr_accessible :title, :content,  :assets_attributes
has_many  :assets
accepts_nested_attributes_for  :assets,  :allow_destroy  => true

Edit app/controller/posts_controller.rb (Note the new and edit part)

def new
@post =
5.times { }


def edit
@post = Post.find(params[:id])
5.times { }

Edit and add the following lines in your app/view/post/_form.html.erb

<%= form_for @post, :html => { :multipart => true } do |f| %>
<%= f.error_messages %>
<%= f.label :title %><br />
<%= f.text_field :title %>

<% f.fields_for :assets do |asset_fields| %>

<% if asset_fields.object.new_record? %>
<%= asset_fields.file_field :asset %>

<% end %>
<% end %>

<% f.fields_for :assets do |asset_fields| %>
<%unless asset_fields.object.new_record? %>


<%= link_to image_tag(asset_fields.object.asset.url(:thumb)), asset_fields.object.asset.url(:original) %>

<%= asset_fields.check_box :_destroy %>

<% end %>
<% end %>

<p><%= f.submit %></p>
<% end %>

That’s All .

In tour terminal run ” rails s” command and open the “http://localhost:3000” in your browser. 🙂

Selecting the files

Uploaded into project 🙂

GitHub Link:
My hearty thanks to:

2 thoughts on “Multiple File uploading in Rails3 Application using “Papercilp”

  1. i do have ma working application ready … bt it is taking only single upload . can i apply this in my application already scaffolded … wot all changes i need to do … plzz guide me a bit ..

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s