form_tag with remote:true不会发出ajax请求

Bjo*_*sen 16 jquery ruby-on-rails ruby-on-rails-3

我的应用程序中有许多不同的表单,所有表单都在使用remote: true.但是一个不能正常工作,因为它不使用ajax调用.

清理后,它看起来像:

<%= form_tag(upload_file_ajax_path, remote: true, multipart: true) do %>
  <%= file_field_tag(:file) %>
  <%= submit_tag("upload") %>
<%end%>
Run Code Online (Sandbox Code Playgroud)

标签看起来像:

<form accept-charset="UTF-8" action="/mycontroller/upload_file_ajax" data-remote="true" enctype="multipart/form-data" method="post">
  <input name="utf8" type="hidden" value="?">
  <input name="authenticity_token" type="hidden" value="1234"></div>
  <input id="file" name="file" type="file">
  <input name="commit" type="submit" value="upload">
</form>
Run Code Online (Sandbox Code Playgroud)

并且路线条目:

post "mycontroller/upload_file_ajax", as: "upload_file_ajax"
Run Code Online (Sandbox Code Playgroud)

但是,在Chrome开发工具中检查通话时,标题显示:

Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Run Code Online (Sandbox Code Playgroud)

而不像其他形式看起来像:

Accept:*/*;q=0.5, text/javascript, application/javascript, application/ecmascript, application/x-ecmascript
Run Code Online (Sandbox Code Playgroud)

我将js文件添加到我的布局中

<%= stylesheet_link_tag    "application", :media => "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
Run Code Online (Sandbox Code Playgroud)

即使我从application.js文件中删除了所有JS代码(除了//= require jquery...),表单也无法正常工作.

我错过了什么?

Jes*_*ott 25

您无法使用AJAX进行文件上传.也就是说,通常.

有一个很棒的宝石,Remotipart,它将这个功能添加到您的远程表单中.

gem 'remotipart', '~> 1.0'
Run Code Online (Sandbox Code Playgroud)

在application.js中

//= require jquery.remotipart
Run Code Online (Sandbox Code Playgroud)