简体   繁体   English

Rails 5 ajax上传文件与remotipart不起作用(发送为HTML而不是JS)

[英]Rails 5 ajax upload file with remotipart not work (send as HTML instead of JS )

Rails 5 and carrierwave Rails 5和载波

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 ? 为什么要以HTML格式发送任何想法?

I solved this issue by replacing 我通过替换解决了这个问题

//= require jquery-ujs

with

//= require rails-ujs

in application.js (Rails 5.1.4) application.js (Rails 5.1.4)

添加“authenticity_token:true”

simple_form_for state,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM