Misha Moroshko
Misha Moroshko

Reputation: 171389

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

I followed this tutorial 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 ?

Upvotes: 1

Views: 2759

Answers (1)

Frankie Roberto
Frankie Roberto

Reputation: 1359

To save the files, you'll need create the Asset models, and also assign the attached_file to those models (which you've also called asset, which could get confusing). In the simple case, this would just look like:

user = User.find(id)
asset = user.assets.new
asset.asset = params[:asset]
asset.save

To do multiple files, simply do a quick loop:

user = User.find(id)
params[:assets].each do |asset|
  asset = user.assets.new
  asset.asset = asset
  asset.save
end

However, you're doing a number of non-standard things here, and making life harder for yourself than it needs to be. Instead of using file_field_tag, you should be using the file_field helper with a Asset instantiated in memory. Eg, if you followed the tutorial you linked to, you'd have something like:

<% form_for @user do |f|
  <% f.fields_for :assets do |asset| %>
    <%= asset.file_field :asset %>
  <% end %>
<% end %>

You'd also need to tell your User model that it's okay to accept child asset models when saving:

def User
  accepts_nested_attributes_for :assets
end

Then in your controller action, you'd just build a few new assets in memory so that the loop in the fields_for works:

def upload
  @user = User.find(id)
  5.times do { @user.assets.build }
end

Hope this makes sense. Keep going - you'll get there.

Upvotes: 1

Related Questions