【发布时间】: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