0

这是我的 _form.html.erb 中的附件代码

<div class="control-group">
      <%= f.label :attachment , :class => 'control-label' %>
      <div class="controls">
        <%= f.file_field :attachment, :class => 'file_field', multiple: 'true' %>
      </div>
    </div>

这是我的模型 invoice_details.rb

class InvoiceDetail < ActiveRecord::Base
    mount_uploader :attachment, AttachmentUploader
  #validates :invoice_number, :supplier_name, :attachment, presence: true # Make sure the owner's name is present.    
  #validates_uniqueness_of :invoice_number
 #validates :invoice_number, length: { maximum: 7 }
 #validates :supplier_name, length: { maximum: 20 }
 #validates :description_of_goods, length: { maximum: 50 }
 #validates :quatity, numericality: true
 #validates :price_per_unit, numericality: true
 #validates :total_amount, numericality: true
end

这里是另一个模型 invoice_file.rb

class InvoiceFile < ActiveRecord::Base
mount_uploader :attachment, AttachmentUploader # Tells rails to use this uploader for this model.
  validates :name, presence: true # Make sure the owner's name is present.  
end
4

1 回答 1

0

您可以使用文件字段输入在表单中添加动态嵌套字段,但这是不好的解决方案,因为这样您可能会在有人尝试上传时遇到超时问题,例如。一次20个文件。

我认为最简单和最快的解决方案是使用“jquery-fileupload-rails”gem。请检查演示应用程序的源代码并将其调整为您的应用程序。

这是一个很好的例子:
https ://github.com/n0ne/Rails-Carrierwave-jQuery-File-Upload

于 2015-03-21T15:27:15.627 回答