Rails 3 and Paperclip problem - How to save the uploaded file(s) to the filesystem ?

237 views
Skip to first unread message

moroshko

unread,
Jun 22, 2011, 12:48:11 AM6/22/11
to Paperclip Plugin
I followed http://www.emersonlackey.com/article/rails-paperclip-multiple-file-uploads
trying to integrate Paperclip into my Rails 3 application.

However, my case looks a bit different from what is described in this
tutorial.

In my case, `User` models are already exist in the database, and I
want to upload file(s) and associate them with the uploader.

Here are the relevant parts of my code:

class User < ActiveRecord::Base
has_many :assets, :foreign_key => "uploader_id"
end

class Asset < ActiveRecord::Base
belongs_to :uploader, :class_name => "User"
has_attached_file :asset, :styles => { :thumb => "100x100#" }
end

The main difference between my case and the tutorial is that the
upload input field is not inside `User`'s form:

# views/lounge/index.html.erb
<%= form_tag('/lounge/upload', :multipart => true) do %>
<input id="uploader_id" name="uploader_id" type="hidden" />
<%= file_field_tag "assets[]", :multiple => true %>
<% end %>

The value of the hidden `uploader_id` input field is controlled by
Javascript.

When the form is submitted the `upload` method is called:

class LoungeController < ApplicationController
def upload
uploader = User.find(params[:uploader_id])
# ??
end
end

What should I do with `params[:assets]` in order to save the uploaded
files to the filesystem and create the corresponding `Asset` models in
the database ?
Reply all
Reply to author
Forward
0 new messages