Harshvardhan
Harshvardhan

Reputation: 83

How to upload multiple files without nested form using Rails 4?

Can anybody tell me how can I implement multiple file upload using carrierwave without taking separate model for files?

I have my model as expense_details.rb and I need to upload multiple receipts for those expenses.

Upvotes: 1

Views: 268

Answers (1)

shanks
shanks

Reputation: 39

You need:

1) a polymorphic attachments resource

2) rails upload gem(https://github.com/carrierwaveuploader/carrierwave)

3) a js multiple uploader(https://blueimp.github.io/jQuery-File-Upload/)

4) easy to use 2 & 3(github.com/tors/jquery-fileupload-rails)

db:

class CreateAttachments < ActiveRecord::Migration
  def change
    create_table :attachments do |t|
      t.string :name
      t.string :link
      t.integer :attachmentable_id
      t.string :attachmentable_type
      t.integer :user_id

      t.timestamps
    end

    add_index :attachments, :user_id
    add_index :attachments, [:attachmentable_id, :attachmentable_type]
  end
end

controller:

class AttachmentsController < ApplicationController
  ...
  def create
    @attachment = Attachment.new(params[:attachment])
    @attachment.name = params[:attachment][:link].original_filename

    if @product_attachment.save
      # do something
    else
      # do something
    end
end

Upvotes: 1

Related Questions