控制器无法检测Ajax请求 [英] Controller can not detect ajax requests

查看:175
本文介绍了控制器无法检测Ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用simple_form创业板和产生的形式,我指定远程:像这样真正的选项:

I am using simple_form gem and generating the form I am specifying the remote:true option like this:

<%= simple_form_for @webinar, validate: true, remote:true do |f| %>

因此​​,输出HTML的格式如下片段:

So, the output html for the form is the following fragment:

<form accept-charset="UTF-8" action="/webinars" class="simple_form new_webinar" data-remote="true" data-validate="true" enctype="multipart/form-data" id="new_webinar" method="post" novalidate="novalidate"> ... </form>

当我检查,使用标准的的form_for 助手被添加的数据远程= 真正的应用于表单时的远程:真正的的选项被使用。正如你可以从生成的HTML看,当我使用的simple_form宝石有这样的属性了。

As I checked, using the standard form_for helper is adding the data-remote='true' to the form when remote:true options is used. And as you can see from the generated html, when I am using the simple_form gem there is such attribute, too.

所以,在我的控制器我有:

So, in my controller I have:

def create
  @webinar = Webinar.new(params[:webinar])

  respond_to do |format|
    if @webinar.save
      format.html { redirect_to @webinar, notice: 'Webinar was successfully created.' }
      format.js
      format.json { render json: @webinar, status: :created, location: @webinar }
    else
      format.html { render action: "new" }
      format.json { render json: @webinar.errors, status: :unprocessable_entity }
    end
  end
end

不过,总是的的format.html 的使用。我在做什么错了?

But, always the format.html is used. What i am doing wrong?

编辑:

我已经使用logger.debug request.format检查什么是实际的格式要求,并在它的日志文件:

I have used logger.debug request.format to check what is the actual format ask for and in the log file it was:

text / html的

text/html

所以,这个问题必须在产生simple_form形式 - 什么都可以错在那里,当我们有数据的远程=真

So, the issue must be in the simple_form generated form - what can be wrong there when we have "data-remote=true"?

推荐答案

我无法相信,我已经失去了这么多时间在试图理解为什么在 simple_form 工作不正常。

I can not believe that I have lost so much time in trying to understand why the simple_form is not working as expected.

最后,看来我在正确的方式做的一切,这个问题被造成的,因为:

Finally, it appears that I have done everything in the right way and the issue was caused because:

AJAX不能用于文件上传。

AJAX can not be used for file uploads.

为了解决我的问题,我只需要添加下面的宝石到我的的Gemfile 的和运行的捆绑安装的命令:

In order to solve my problem I simply have to add the following gem into my Gemfile and run the bundle install command:

gem 'remotipart', '~> 1.0'

然后添加下面的一行在我的的application.js 的文件:

// =需要jquery.remotipart

//= require jquery.remotipart

有关的更多信息:

  1. remotipart宝石
  2. 如何使用jQuery
  3. 来上传多个文件
  1. remotipart gem
  2. How to upload multiple files using jQuery

这篇关于控制器无法检测Ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆