remotipart icon indicating copy to clipboard operation
remotipart copied to clipboard

Form with remote: true still send as HTML

Open Merff opened this issue 9 years ago • 13 comments

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 ?

Merff avatar Dec 29 '16 09:12 Merff

@Merff do you have = csrf_meta_tags in your layout?

stereodenis avatar Jan 16 '17 17:01 stereodenis

@stereodenis yes, I have

Merff avatar Jan 17 '17 14:01 Merff

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

stereodenis avatar Jan 17 '17 21:01 stereodenis

@stereodenis why? how I can add this params?

Merff avatar Jan 18 '17 05:01 Merff

I'm having this same issue.

invisiwill avatar Feb 27 '17 00:02 invisiwill

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

Merff avatar Mar 09 '17 09:03 Merff

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

pedroadame avatar Apr 03 '17 11:04 pedroadame

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

Merff avatar Apr 03 '17 12:04 Merff

@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 avatar Apr 03 '17 12:04 pedroadame

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

pedroadame avatar Apr 04 '17 10:04 pedroadame

I am having the same issue any solution @JangoSteve ?

vkeziah avatar Aug 25 '17 06:08 vkeziah

@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.

matissg avatar Oct 16 '17 04:10 matissg

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.

mcfoton avatar Jul 03 '18 14:07 mcfoton