ruby-on-rails-3carrierwavefog

How do I know if Carrierwave is initialized and uploader is mounted?


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

Solution

  • 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