我正在设置一个上传器(我之前已经完成了一百万次没有任何问题)但是当我提交表单时,似乎载波与挂载的列没有任何交互。表单提交成功,但由于某种原因,已安装的列只是默认为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
答案 0 :(得分:0)
这是你的问题
"file_location"=>"my_file.pdf"
如果您已在载波上传器中装入file_location
列。
Carrierwave希望它是file
或remote_url
外观here缓存的预期输入是什么,这基本上是在载波将文件上传到所需位置之前的第一步
在params中传递一个文件,我相信载波将按预期工作
答案 1 :(得分:0)
Class.include?(CarrierWave)=>如果您的CarrierWave包含在班级中,则返回true。
Class.uploaders =>它将为您提供带有上载程序的已装载字段的哈希。即:{:image => FileUploader}