【问题标题】:Direct upload with Cloudinary and Carrierwave使用 Cloudinary 和 Carrierwave 直接上传
【发布时间】:2013-08-04 09:02:18
【问题描述】:

我一直按照Cloudinary 提供的说明进行操作,但无法进行直接上传。更复杂的是,我的图像上传是一个多态类,通常是嵌套形式。

我同时使用 Cloudinary 和 Carrierwave gem。在非直接设置中,一切正常,但是如果一次上传的图像过多(可能经常出现这种情况),独角兽会超时

下面是添加文件上传的部分。它嵌套在多个不同的表单中,用户可以动态添加和删除字段。根据说明,我尝试将= f.file_field :asset= f.hidden_field :asset_cache 替换为= cl_image_upload :asset,但是,这会引发错误:wrong number of arguments (1 for 2..3)。添加第二个参数时,它会附加到生成的 HTML 中的data-cloudinary-field。此外,当添加图像并且没有将参考附加到记录时,不会进行上传。

_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

以下是相关文件:

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

编辑:添加图像控制器

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

【问题讨论】:

  • 我查看了thisthis 但无法弄清楚。
  • 我还仔细检查了 jQuery 插件似乎正在加载。我通过 application.js //= require cloudinary 添加了它
  • 我需要做的就是在cl_image_upload 助手前面添加f.。因此f.cl_image_upload :asset 现在为我工作。

标签: ruby-on-rails carrierwave cloudinary


【解决方案1】:

以下文档部分描述了如何在 Ruby on Rails 应用程序中使用从浏览器直接上传图像。确保以正确的顺序包含 jQuery 和所需的插件,并添加所需的 Javascript 配置。

http://cloudinary.com/documentation/rails_image_upload#direct_uploading_from_the_browser

使用接受输入字段名称的cl_image_upload_tag 辅助方法嵌入执行直接图像上传的文件输入字段。

使用 CarrierWave 时,可以使用 cl_image_upload 辅助方法。直接调用此方法时,您需要传递对象名称和属性(就像使用其他 Rails 视图辅助方法,如 text_field_tagtext_field 一样)。或者,您可以将它与 Rails 的标准表单构建器一起使用。 假设您的模型称为entity,并且您的带有已安装 CarrierWaver 上传器的属性称为asset,则以下视图代码应嵌入签名文件输入字段,以便从浏览器直接上传:

= form_for(:entity) do |f|
  = f.cl_image_upload(:asset)

此外,如下所示,您可以使用present? 来检查资产是否存在并将CarrierWave 属性直接传递给cl_image_tag。或者,您可以使用 CarrierWave 标准版本,而不是使用 cl_image_tag 动态构建图像 URL。

- if f.object && f.object.asset.present?
  .image-box
    = cl_image_tag(f.object.asset, :transformation => 'hint', alt: f.object.asset.filename.to_s)

如果文件输入字段已成功添加到您的视图中,但未执行直接上传,则应验证控制台中没有 Javascript 错误,并且所有 jQuery 插件均已正确包含。

【讨论】:

    猜你喜欢
    • 2017-08-30
    • 2018-06-23
    • 1970-01-01
    • 2015-12-05
    • 2016-12-22
    • 2014-01-19
    • 1970-01-01
    • 2023-03-09
    • 2016-12-19
    相关资源
    最近更新 更多