How to upload file via ajax with Rails 4?

0

I want to download a pdf file to process on my controller and then return it as it gets rendered in the backend , I would like a loading bar or circle to appear. However, I'm having trouble sending the file asynchronously. How do I send an asynchronous file through Rails 4?

    
asked by anonymous 02.08.2016 / 06:03

1 answer

2

link

link

>

There are several solutions to do this. Today I use the solution to CarrierWave + DropDzone

Philip, The HTTP POST / PUT method allows you to make a client & server for binary files.

In order to make your controller accept this type of file you will have to prepare a MODEL to capture this file to make it the desired file "PDF, JPG, PNG ....

model / asset.rb

class Asset < ActiveRecord::Base
  belongs_to :assetable, polymorphic: true
  mount_uploader :file, FileUploader
end
remember to look at how to configure the carrierwave

link

model / nfes.rb

has_one :asset, :class_name => "Asset", as: :assetable, dependent: :destroy

NfesController.rb

respond_to :html, :xml, :json, :js
...
private
def nfe_params
  params.require(:nfe).permit(:build_id, :due_at, asset_attributes:[:id, :file])
end

views / new.html.slim

form_for @nfe, html: { multipart: true, class: "dropzone"}, method: :post do |f| 
  div.fallback
  - f.object.build_asset if f.object.asset.blank? 
  = f.fields_for :asset  do |asset|
  = asset.file_field :file
  = f.submit "Upload"
    
02.08.2016 / 06:44