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

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

问题描述

我正在使用 simple_form gem 并生成我指定 remote:true 选项的表单,如下所示:

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

因此,表单的输出 html 是以下片段:

<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 助手正在添加 data-remote=当使用 remote:true 选项时,'true' 到表单.从生成的 html 中可以看出,当我使用 simple_form gem 时,也有这样的属性.

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

def 创建@webinar = Webinar.new(params[:webinar])response_to do |格式|如果@webinar.saveformat.html { redirect_to @webinar,注意:'Webinar 已成功创建.'}格式.jsformat.json { 渲染 json:@webinar,状态::已创建,位置:@webinar }别的format.html { 渲染动作:新"}format.json { 渲染 json:@webinar.errors,状态::unprocessable_entity }结尾结尾结尾

但是,总是使用 format.html.我做错了什么?

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

<块引用>

文本/html

因此,问题必须出在 simple_form 生成的表单中 - 当我们使用data-remote=true"时,会出现什么问题?

解决方案

我不敢相信我已经浪费了这么多时间试图理解为什么 simple_form 没有按预期工作.>

最后,看来我已经以正确的方式完成了所有事情,并且导致问题的原因是:

<块引用>

AJAX 不能用于文件上传.

为了解决我的问题,我只需将以下 gem 添加到我的 Gemfile 中并运行 bundle install 命令:

gem 'remotipart', '~>1.0'

然后在我的 application.js 文件中添加以下行:

<块引用>

//= 需要 jquery.remotipart

更多信息:

  1. remotipart gem
  2. 如何使用 jQuery 上传多个文件

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| %>

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>

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

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

EDIT:

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

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

解决方案

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 can not be used for file uploads.

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'

Then add the following line in my application.js file:

//= require jquery.remotipart

More information about:

  1. remotipart gem
  2. How to upload multiple files using jQuery

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

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