How to upload file via ajax with Rails 4?

Asked

Viewed 63 times

0

I want to transfer a pdf file to process on my controller and then return it while it is being processed on backend, I would like a slider or click circle to appear. However, I am having trouble sending the file asynchronously. How do I send an asynchronous file through Rails 4?

1 answer

2


https://www.sitepoint.com/asynchronous-file-uploads-rails/

https://github.com/JangoSteve/remotipart

https://medium.com/rails-ember-beyond/ajax-file-upload-in-rails-using-dropzone-and-carrierwave-6f5436fdfdb0#. 4paxrz751

There are several solutions to do this. Today I use the solution Carrierwave + Dropdzone


Felipe, The HTTP POST/PUT method allows you to make a client & server connection of binary files.

In order to get your controller to accept this type of file you will have to prepare a MODEL to capture this file to transform it into 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 set up the carrierwave

http://railscasts.com/episodes/253-carrierwave-file-uploads

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"
  • My question is how does it move up the file and when I click on submit, it change the content of the page, you could give an example here?

  • @Breno Perucchi avoid posting only links as answers, put the relevant parts in your reply.

  • @magicHat The question was very general. It did not involve, how to solve this or I’m having trouble with it and yes I would like a solution to it. I didn’t know links weren’t appropriate for answers.

  • I think when it is so better to put in the comment... I don’t know... But the fact of putting the links, it is because it leaves the air, there is broken,,,

  • @Magichat posted something consistent

  • 1

    Look at the difference...

  • @Felipeavelar worked?

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.