RailsCasts Pro episodes are now free!

Learn more or hide this

krishna varma's Profile

GitHub User: buddaraju

Comments by krishna varma

Avatar

how to upload multiple files using carrierwave
hi
i am using carrierwave gem for file upload. It is working fine with single upload, but how to upload multiple files please help me.

This is my form:

<%= form_for(@invoice_detail , html: {class: 'form-horizontal', role: 'form' }) do |f| %>
 <div class="field">
        <%= f.label :invoice_number, :class => 'control-label' %>
            <div class="controls">
    <%= f.text_field :invoice_number, :class => 'text_field', :required => true, :maxlength => 15, :placeholder =>'15 Alpha Numeric with special Characters', :presence => "check invoice num"   %>
  </div>
  </div>
  <div class="control-group">
      <%= f.label :attachment, :multiple => true , :class => 'control-label' %>
      <div class="controls">
        <%= f.file_field :attachment, :class => 'file_field', :required => true %>
      </div>
    </div>
    <div class="form-actions1">
    <%= f.submit  :class => 'btn btn-primary' %>
    <%#= f.submit "cancel", :class => 'btn btn-danger', method: :delete, data: { confirm: 'Are you sure you want to cancel' } %>
    <%= link_to "Cancel", invoice_details_path(), :class => 'btn btn-danger', data: { confirm: 'Are you sure you want to cancel' } %>

  </div>
<% end %>

attachment_uploader.rb:

class AttachmentUploader < CarrierWave::Uploader::Base

  storage :file

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end
  def extension_white_list
    %w(xlsx pdf doc htm html docx png jpg jpeg xls)
  end

  def filename
  "#{model.invoice_number}.#{file.extension}" if original_filename.present?
end

end

I used http://richonrails.com/articles/allowing-file-uploads-with-carrierwave for refernce.