我一直按照Cloudinary提供的说明进行操作,但无法直接上传。更复杂的是,我的图片上传是一个多态类,通常是嵌套形式。
我正在使用 Cloudinary 和 Carrierwave gems。在非直接设置中,一切正常,但是如果一次上传太多图像,独角兽会超时(可能经常出现这种情况)
下面是添加文件上传的部分。它嵌套在多个不同的表单中,用户可以动态添加和删除字段。根据说明,我尝试将= f.file_field :asset
and= f.hidden_field :asset_cache
替换为= cl_image_upload :asset
,但是,这会引发错误:wrong number of arguments (1 for 2..3)
。添加第二个参数时,它会附加到data-cloudinary-field
生成的 HTML 中。此外,当添加图像并且没有将参考附加到记录时,不会进行上传。
_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
以下是相关文件:
图片.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
编辑:添加图像控制器
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