【问题标题】:how to Display the multiple files array were uploaded by CarrierWave?如何显示由 CarrierWave 上传的多个文件数组?
【发布时间】:2016-11-02 09:42:43
【问题描述】:

我用gem 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

我该如何解决?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-4 sqlite carrierwave


    【解决方案1】:

    我在官方issuehere发现了同样的问题,我解决了问题

    只需将serialize :documents 添加到模型中,所有问题都消失了

    【讨论】:

      【解决方案2】:

      这取决于您的数据库格式:

      如果你有一个没有 ActiveRecord 的数据库(例如 SQLite),你必须添加:

      mount_uploaders :documents, DocumentUploader    
      serialize :docuements, JSON # If you use SQLite, add this line.
      

      如果你有一个带有 ActiveRecord 的数据库(例如 PostgreSQL、MySQL),你只需要添加:

      mount_uploaders :documents, AvatarUploader    
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-12-05
        • 1970-01-01
        • 2023-03-14
        • 2012-05-22
        • 2013-09-15
        • 1970-01-01
        • 2018-07-08
        • 1970-01-01
        相关资源
        最近更新 更多