JangoSteve / remotipart

Rails jQuery file uploads via standard Rails "remote: true" forms.
http://os.alfajango.com/remotipart
Other
1k stars 215 forks source link

Form with remote: true still send as HTML #174

Open Merff opened 7 years ago

Merff commented 7 years ago

Rails 5 and carrierwave I add:

gem 'remotipart', '~> 1.3.1'

and

//= require jquery.remotipart

I have the form:

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

The output for that form:

<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 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 as HTML, any ideas ?

stereodenis commented 7 years ago

@Merff do you have = csrf_meta_tags in your layout?

Merff commented 7 years ago

@stereodenis yes, I have

stereodenis commented 7 years ago

@Merff but your form is not submitted by remotipart (it has no remotipart params)

Merff commented 7 years ago

@stereodenis why? how I can add this params?

invisiwill commented 7 years ago

I'm having this same issue.

Merff commented 7 years ago

In my case the error was from jquery handler - "onchange"

pedroadame commented 7 years ago

I'm facing the same problem. Any solution? @stereodenis

Merff commented 7 years ago

@pedroadame check jquery handlers, in my case problem was from handler.

pedroadame commented 7 years ago

@Merff I'm not using any jQuery handler. My form is displayed via AJAX in a vex (in case you don't know, is a JS modal library) dialog, and submitted via AJAX from there. Suddenly it started saying 'Invalid authenticity token', which was fixed by adding authenticity_token: true to the form_for, but it always process the request as HTML, then crashes on the respond_to :js line.

Currently I'm building the form again from scratch just to see what broken it, because I cannot figure out.

pedroadame commented 7 years ago

@Merff I solved my issue, it turned out to be something with vex. Stopped using it, it worked flawlessly.

vkeziah commented 7 years ago

I am having the same issue any solution @JangoSteve ?

matissg commented 7 years ago

@vkeziah I ended up using ufujs-rails gem. Works like charm in my case with AJAX modal and file upload. In addition I use this approach to generate unique file names.

mcfoton commented 6 years ago

Rails 5.2.0 here. It helped me to switch from //= require rails_ujs to //= require jquery_ujs but I'm far from being sure that's a proper way to go.