I've been following the instructions provided by Cloudinary, but have not been able to get direct uploading working. To further complicate things my image upload is a polymorphic class and is usually in a nested form.
I'm using both the Cloudinary and Carrierwave gems. In the non-direct setup everything works properly however unicorn times out if there are too many images being uploaded at once (which may frequently be the case)
Below is the partial that adds a file upload. It is nested in multiple different forms and the user can add and remove fields dynamically. Per the instructions, I tried to replace = f.file_field :asset
and = f.hidden_field :asset_cache
with = cl_image_upload :asset
, however, this throws an error of: wrong number of arguments (1 for 2..3)
. When adding a second parameter it is appended to data-cloudinary-field
in the generated HTML. Additionally no upload takes place when an image is added and no reference is attached to the record.
_image_fields.html.haml
.image-field-group
.field
= f.label :asset, "Image"
= cl_image_upload :asset
/ = f.file_field :asset
/ = f.hidden_field :asset_cache
- if f.object && f.object.asset && f.object.asset.filename
.image-box
= cl_image_tag(f.object.asset.filename.to_s, :transformation => 'hint', alt: f.object.asset.filename.to_s)
.remove-fields
= link_to_remove_fields f
Here are the associated files:
image.rb
class Image < ActiveRecord::Base
default_scope order('images.id ASC')
attr_accessible :asset,
:asset_cache
belongs_to :imageable, polymorphic: true
mount_uploader :asset, ImageUploader
end
image_uploader.rb
class ImageUploader < CarrierWave::Uploader::Base
include Cloudinary::CarrierWave
def extension_white_list
%w(jpg jpeg gif png)
end
end
EDIT: Added Images Controller
images_controller.rb
class ImagesController < ApplicationController
before_filter :load_imageable
def index
@images = @imageable.images
end
def new
@image = @imageable.images.new
end
def create
@image = @imageable.images.new(params[:image])
if @image.save
redirect_to @imageable, notice: "Image created."
else
render :new
end
end
private
def load_imageable
resource, id = request.path.split('/')[1, 2]
@imageable = resource.singularize.classify.constantize.find(id)
end
end
//= require cloudinary
– Eric Norcrossf.
in front of thecl_image_upload
helper. Thusf.cl_image_upload :asset
is now working for me. – Eric Norcross