【问题标题】:How do I know if Carrierwave is initialized and uploader is mounted?我如何知道 Carrierwave 是否已初始化并且上传器是否已安装?
【发布时间】:2014-02-06 06:01:02
【问题描述】:

我正在设置一个上传器(我之前已经完成了一百万次没有任何问题)但是当我提交表单时,carrierwave 似乎与已安装的列没有任何交互。表单提交成功,但由于某种原因,mounted 列默认为nil。下面是我的服务器日志以及我的设置。我的问题是,我怎么知道载波实际上已经初始化并且列安装到上传器?

服务器日志:

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)

上传者

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 初始化器:

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

型号:

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

【问题讨论】:

    标签: ruby-on-rails-3 carrierwave fog


    【解决方案1】:

    Class.include?(CarrierWave) => 如果您的 CarrierWave 包含在您的课程中,这将返回 true。

    Class.uploaders => 它将为您提供带有上传器的已安装字段的哈希值。即:{:image => FileUploader}

    【讨论】:

      【解决方案2】:

      这是你的问题 "file_location"=&gt;"my_file.pdf"

      如果您在载波上传器中安装了file_location 列。

      Carrierwave 期望它是 fileremote_url 看看 here 缓存的预期输入是什么,这基本上是carrierwave 将文件上传到所需位置之前的第一步

      在参数中传递一个文件,我相信载波会按预期工作

      【讨论】:

      • 我的表单看起来像= f.file_field :file_location,它反过来应该在参数中传递文件..根据上面的服务器日志(和你的观点)但是,它似乎并没有真正传递文件,只是文件文件名。这就是为什么我要质疑上传器是否实际已安装.. 想法?
      • 你的表单是否是多部分选项,你是否也尝试通过 ajax 提交文件
      • 更新:通过许多小时的工作,我发现这个问题与我的Document 模型有关联这一事实有关。当模型不与任何其他模型关联时,上传器按预期工作,但是一旦我将它(标准或多态)与另一个模型相关联,上传器不再工作..不确定为什么会发生这种情况
      • @RemyBartolotta:这很奇怪,看起来任何生成的方法名称都不应该发生冲突或任何事情。生成的表单标记有什么不同吗?
      • @ash-wilson: 型号名称 Document 会不会有冲突?生成的表单标记看起来没有任何不同。我开始怀疑我安装 gem 时可能出现了错误。我正在删除 gem 并重新安装
      猜你喜欢
      • 1970-01-01
      • 2016-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-23
      • 1970-01-01
      • 2011-10-20
      • 2017-06-01
      相关资源
      最近更新 更多