如何显示 CarrierWave 上传的多个文件数组?
how to Display the multiple files array were uploaded by CarrierWave?
我用gemCarrierWave上传了多个文件,我可以上传成功,但我无法下载并显示每个文件的地址。
我尝试使用控制台显示我上传文件的数组,我得到了这个:
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
我该如何解决?
我在官方issuehere上发现了同样的问题,我解决了问题
只需将 serialize :documents
添加到模型,所有问题都消失了
这取决于您的数据库格式:
如果您的数据库没有 ActiveRecord(例如 SQLite),您必须添加:
mount_uploaders :documents, DocumentUploader
serialize :docuements, JSON # If you use SQLite, add this line.
如果你有一个带有 ActiveRecord 的数据库(例如 PostgreSQL,MySQL),你只需要添加:
mount_uploaders :documents, AvatarUploader
我用gemCarrierWave上传了多个文件,我可以上传成功,但我无法下载并显示每个文件的地址。
我尝试使用控制台显示我上传文件的数组,我得到了这个:
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
我该如何解决?
我在官方issuehere上发现了同样的问题,我解决了问题
只需将 serialize :documents
添加到模型,所有问题都消失了
这取决于您的数据库格式:
如果您的数据库没有 ActiveRecord(例如 SQLite),您必须添加:
mount_uploaders :documents, DocumentUploader
serialize :docuements, JSON # If you use SQLite, add this line.
如果你有一个带有 ActiveRecord 的数据库(例如 PostgreSQL,MySQL),你只需要添加:
mount_uploaders :documents, AvatarUploader