我使用宝石CarrierWave上传了多个文件,我可以上传成功,但我无法下载并显示每个文件的地址。
我尝试使用控制台显示上传文件的数组,我得到了这个:
2.2.4 :013 > Legislation.find(14).documents.each do |doc|
2.2.4 :014 > puts doc
2.2.4 :015?> end
Legislation Load (0.4ms) SELECT "legislations".* FROM "legislations" WHERE "legislations"."id" = ? LIMIT 1 [["id", 14]]
/uploads/legislation/documents/14/%5B%2267490.jpg%22%2C%20%2214536943_1300076803359446_1963523476_o.jpg%22%2C%20%221436764358456.jpg%22%2C%20%22ch_09b.ppt%22%5D
=> [#<DocumentUploader:0x007fc5c7b6bcc0 @model=#<Legislation id: 14, title: "ascsf", created_at: "2016-11-02 08:51:39", updated_at: "2016-11-02 08:51:39", attachment: nil, documents: "[\"67490.jpg\", \"14536943_1300076803359446_196352347...">, @mounted_as=:documents, @storage=#<CarrierWave::Storage::File:0x007fc5c7b6bbf8 @uploader=#<DocumentUploader:0x007fc5c7b6bcc0 ...>>, @file=#<CarrierWave::SanitizedFile:0x007fc5c7b6b2e8 @file="/Users/ChamperWu/projects/nsysu_mem/public/uploads/legislation/documents/14/[\"67490.jpg\", \"14536943_1300076803359446_1963523476_o.jpg\", \"1436764358456.jpg\", \"ch_09b.ppt\"]", @original_filename=nil, @content_type=nil>, @versions={}>]
当我上传一个文件时,文件地址错误,文件名将为
%5B%2267490.jpg%22%2C%20%
不是真名
67490.jpg
控制器:
def legislation_params
params.require(:legislation).permit(:title, :attachment, {documents: []})
end
型号:
class Legislation < ActiveRecord::Base
mount_uploader :attachment, AttachmentUploader
mount_uploaders :documents, DocumentUploader
validates :title, presence: true
end
迁移:
class AddDocmentToLegislations < ActiveRecord::Migration
def change
add_column :legislations, :documents, :string, array: true, default: []
end
end
我该如何解决?
答案 0 :(得分:1)
我在官方问题here中找到了同样的问题,我解决了问题
只需将serialize :documents
添加到模型中,所有问题都将消失
答案 1 :(得分:0)
这取决于您的数据库格式:
如果您的数据库没有ActiveRecord(例如SQLite),则必须添加:
mount_uploaders :documents, DocumentUploader
serialize :docuements, JSON # If you use SQLite, add this line.
如果你有一个ActiveRecord数据库(例如PostgreSQL,MySQL),你只需要添加:
mount_uploaders :documents, AvatarUploader