0

I'm setting up an uploader (which I've done without any problems a million times before) however when I submit the form, it doesn't appear that carrierwave has any interaction with the mounted column. The form submission is successful, however for some reason the mounted column simply defaults to nil. Below is my server log as well as my setup. My question is, how do i know that carrierwave has actually been initialized and the column mounted to the uploader?

server log:

Processing by DocumentsController#create as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"oCw+WOBL7OIUjaOLh9Z0VR1g5KxErMBtLATmMQI6OJk=", "document"=>{"title"=>"Spec Sheet", "category"=>"Spec Sheet", "file_location"=>"my_file.pdf"}, "commit"=>"Create Document", "product_id"=>"1"}
Product Load (0.3ms)  SELECT "products".* FROM "products" WHERE "products"."id" = $1 LIMIT 1  [["id", "1"]]
(0.1ms)  BEGIN
SQL (4.0ms)  INSERT INTO "documents" ("category", "created_at", "documentable_id", "documentable_type", "file_location", "title", "updated_at") VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING "id"  [["category", "Spec Sheet"], ["created_at", Thu, 06 Feb 2014 05:29:31 UTC +00:00], ["documentable_id", 1], ["documentable_type", "Product"], ["file_location", nil], ["title", "Spec Sheet"], ["updated_at", Thu, 06 Feb 2014 05:29:31 UTC +00:00]]
(6.3ms)  COMMIT
Redirected to http://localhost:3000/products/1
Completed 302 Found in 19ms (ActiveRecord: 10.7ms)

Uploader

class DocumentUploader < CarrierWave::Uploader::Base

# include CarrierWaveDirect::Uploader

# Include the Sprockets helpers for Rails 3.1+ asset pipeline compatibility:
  include Sprockets::Helpers::RailsHelper
  include Sprockets::Helpers::IsolatedHelper

  include CarrierWave::MimeTypes
  process :set_content_type
# Override the directory where uploaded files will be stored.
# This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

# Add a white list of extensions which are allowed to be uploaded.
# For images you might use something like this:
  def extension_white_list
    %w(pdf doc)
  end
 end

carrierwave.rb initializer:

CarrierWave.configure do |config|
 if Rails.env.production?
  config.storage = :fog
  config.fog_credentials = {
    provider: 'AWS',
    aws_access_key_id: 'xxx',
    aws_secret_access_key: 'xxx'
  }

  config.fog_directory  = 'my_file_location'
  config.fog_public     = false

 else
  config.storage = :file
  config.enable_processing = false
 end
end

model:

class Document < ActiveRecord::Base
 attr_accessible :file_location, :category, :title, :documentable_type, :documentable_id

 mount_uploader :file_location, DocumentUploader

 belongs_to :documentable, polymorphic: true
end

2 Answers2

1

Class.include?(CarrierWave) => This will return true if your CarrierWave is included in to your class.

Class.uploaders => It will give you hash of mounted fields with uploaders.i.e: {:image => FileUploader}

Abhishek
  • 93
  • 10
0

Here is your problem "file_location"=>"my_file.pdf"

if you have mounted file_location column in the carrierwave uploader .

Carrierwave expect it to be a file or remote_url look here what is the expected input to cache which is basically first step before carrierwave upload the file to the desired location

Pass a file in params and I believe the carrierwave would work as expected then

Viren
  • 5,812
  • 6
  • 45
  • 98
  • my form looks like `= f.file_field :file_location`, which in turn should pass the file in params.. based on the server log above (and to your point) however, it doesnt seem to actually pass the file, simply the file file name. This is why I'm questioning whether or not the uploader is actually mounted.. thoughts? – Remy Bartolotta Feb 06 '14 at 14:23
  • does you form is multipart options also are u trying to submit file via ajax – Viren Feb 06 '14 at 15:44
  • UPDATE: through many hours of working on this, I have discovered that the issue has something to do with the fact that my `Document` model has an association. When the model is not associated with any other, the uploader works as expected, however once I associate it (standard or polymorphic) with another model, the uploader no longer works..Not sure why this happens – Remy Bartolotta Feb 06 '14 at 16:43
  • @RemyBartolotta: That's strange, it doesn't look like any of the generated method names should collide or anything. Is the generated form markup any different? – Ash Wilson Feb 06 '14 at 18:11
  • @ash-wilson: Could there be a colission with the model name Document? the generated form markup doesn't appear any different. I am beginning to suspect that there may have been an error when I installed the gem. I'm removing the gem and reinstalling – Remy Bartolotta Feb 06 '14 at 20:56
  • Ash Wilson is right highly unlikely that the polymorphic does or would collide with any uploader method of carrierwave since I have use them in my application having polymorhic association and they work good and yes even my Model name are define like `Document` or `DocumentMapper` etc I dont see a problem with that can you add some other reference like html markup of your code – Viren Feb 07 '14 at 05:50
  • Interestingly enough, the trouble was the name of my uploader, 'DocumentUploader'. I renamed it to pdf_uploader and everything worked as it should. This doesn't really make any sense to me, as I can't image any collision with that name, but everything works fine after the renaming. – Remy Bartolotta Feb 11 '14 at 18:57