jesii7
2/1/2012 - 2:58 PM

Paperclip multiple attachment issues

Paperclip multiple attachment issues

##### User model

class User < ActiveRecord::Base
  # Devise stuff removed

  # Setup accessible (or protected) attributes for your model
  # I don't believe the attr_accessible :asset is required in my caes, but it's here anyway
  attr_accessible :name, :email, :password, :password_confirmation, :remember_me, :asset

  validates_presence_of :name
  validates_uniqueness_of :name, :email, :case_sensitive => false

  has_many :assets
  accepts_nested_attributes_for :assets
end

##### Asset model

class Asset < ActiveRecord::Base

  belongs_to :users
  has_attached_file :asset, :path => (Rails.root + "files/:id").to_s
end

##### User Controller
class UsersController < ApplicationController
  
  # Handle uploading new worker information
  def upload_workers
    @user = User.find(:all, :conditions => [ "name = 'Administrator'"])[0]
    logger.debug "User is #{@user}"
    @asset = @user.assets.build()
    logger.debug "Asset is #{@asset}"
  end

  def process_workers_file
    @user = User.find(:all, :conditions => [ "name = 'Administrator'"])[0]
    @asset = @user.assets.build(params[:asset])
    @user.save!
  end

end

##### View - upload_workers.html.haml
%h1 Upload New Worker File
= form_for @asset, :url => '/user_process_workers_file', :html => {:multipart => true}, :method => 'get' do |f|
  -#-if @user.errors.any?
    -# Error explanation
    -#%h2= "#{pluralize(@user.errors.count, "error")} prohibited this user from being saved:"
    -#%ul
      -#-@user.errors.full_messages.each do |msg|
        -#%li= msg
  .field
    =f.label "Description"
    =f.text_field :description
  .field
    =f.label "File"
    =f.file_field :file_name
  .actions
    =f.submit "Upload worker file"

### Log output
Started POST "/user_process_workers_file" for 127.0.0.1 at 2012-02-01 06:50:50 -0800
  Processing by UsersController#process_workers_file as HTML
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"5SLEdsIdjTxGH8RI3Y0BRXD5AHM5iKIMWcNdYaGL49I=", "asset"=>{"description"=>"Test 8 new logger", "file_name"=>"AAA_paperclip_testing.csv"}, "commit"=>"Upload worker file"}
  User Load (1.2ms)  SELECT "users".* FROM "users" WHERE (name = 'Administrator')
  SQL (0.2ms)  SELECT 1 FROM "users" WHERE (LOWER("users"."name") = LOWER('Administrator')) AND ("users".id <> 1) LIMIT 1
  SQL (0.1ms)  SELECT 1 FROM "users" WHERE (LOWER("users"."email") = LOWER('jseidel@edpci.net')) AND ("users".id <> 1) LIMIT 1
  SQL (0.1ms)   SELECT name
 FROM sqlite_master
 WHERE type = 'table' AND NOT name = 'sqlite_sequence'
  AREL (0.3ms)  INSERT INTO "assets" ("description", "file_name", "file_size", "content_type", "created_at", "updated_at", "user_id") VALUES ('Test 8 new logger', 'AAA_paperclip_testing.csv', NULL, NULL, '2012-02-01 14:50:51.582432', '2012-02-01 14:50:51.582432', 1)
[paperclip] Saving attachments.
Rendered users/process_workers_file.html.haml within layouts/application (14.1ms)
Completed 200 OK in 272ms (Views: 25.1ms | ActiveRecord: 1.9ms)