Rails 5 ajax download file with remotipart not working (send as HTML instead of JS)

Rails 5 and the carrier wave

I add:

gem 'remotipart', '~> 1.3.1'

and

//= require jquery.remotipart

I have a form:

= simple_form_for state, remote: true do |f|
  = f.input :wsr

The output for this form is:

<form class="simple_form" novalidate="novalidate" id="edit_odc_state_73" enctype="multipart/form-data" action="/odc_states/73" accept-charset="UTF-8" data-remote="true" method="post">

But when I submit the form:

Started PATCH "/odc_states/73" for 127.0.0.1 at 2016-12-29 16:02:56 +0700
Processing by OdcStatesController#update as HTML
  Parameters: {"utf8"=>"✓", "odc_state"=>{"wsr"=>#<ActionDispatch::Http::UploadedFile:0x007f67ba393338 @tempfile=#<Tempfile:/tmp/RackMultipart20161229-10725-1r3sn5z.txt>, @original_filename="test1.txt", @content_type="text/plain", @headers="Content-Disposition: form-data; name=\"odc_state[wsr]\"; filename=\"test1.txt\"\r\nContent-Type: text/plain\r\n">}, "id"=>"73"}
Can't verify CSRF token authenticity.
Completed 422 Unprocessable Entity in 1ms (ActiveRecord: 0.0ms)

ActionController::InvalidAuthenticityToken (ActionController::InvalidAuthenticityToken):

Why send HTML, any ideas?

+4
source share
3 answers

I solved this problem by replacing

//= require jquery-ujs

with

//= require rails-ujs

in application.js(Rails 5.1.4)

+1
source

add "authenticity_token: true"

simple_form_for state,
0
source

js-. , JQuery "onchange". js.

-1

Source: https://habr.com/ru/post/1665218/


All Articles