为什么Rails UJS是ajax:成功绑定被调用两次?

时间:2022-02-22 13:05:17

I have a simple form:

我有一个简单的形式:

= form_for(posts_path, :id => "new_post", :remote => true) do
  = text_field_tag "post[input]"
  = submit_tag "Post!"

I have bound a callback to the ajax:success event:

我绑定了一个回调ajax:success事件:

$("form#new_post").bind("ajax:success", function(xhr, data, status){
  alert("Post Created!");
});

When I click the Post! button, the Post Created comes up twice. Why?

当我点击帖子!按钮,Post Created出现两次。为什么?

I'm using Rails 3.1 which by default is using jquery-ujs.

我正在使用Rails 3.1,默认情况下使用的是jquery-ujs。

3 个解决方案

#1


9  

That is because your page is loading jquery_ujs code twice in development mode when precompiled assets exist in /public/assets.

这是因为当/ public / assets中存在预编译资产时,您的页面在开发模式下两次加载jquery_ujs代码。

In development mode javascript requries are loaded with separate tags: jquery, jquery_ujs.js, myscripts.js and finally applications.js. The problem happens when precompiled application.js exists and is used from /public/assets - it contains compilation of all previous files. This is triggered by assets:precompile rake task.

在开发模式下,javascript requries加载了单独的标记:jquery,jquery_ujs.js,myscripts.js,最后是applications.js。当预编译的application.js存在并且从/ public / assets使用时会发生问题 - 它包含所有以前文件的编译。这由资产触发:预编译rake任务。

The solution is to remove /public/assets directory on development then application.js is used (from /app/assets/javascript) which doesn't include previous files. Generally doesn't use assets:precompile rake task on development.

解决方案是在开发时删除/ public / assets目录,然后使用application.js(来自/ app / assets / javascript),它不包含以前的文件。一般不使用资产:预编译rake任务开发。

Update

更新

Adding config.serve_static_assets = false to development.rb also solves problem for me without worrying about /public/assets.

将config.serve_static_assets = false添加到development.rb也为我解决了问题,而不必担心/ public / assets。

#2


5  

A similar thing happened to me upgrading an application from Rails 3.0 to 3.1, it was my mistake. In your

类似的事情发生在我将应用程序从Rails 3.0升级到3.1时,这是我的错误。在你的

app/assets/javascripts/application.js

check that your are not calling twice the rails helpers, i have troubles using

检查你是不是两次铁路助手,我有麻烦使用

//= require_tree .

i have removed this and just left

我删除了这个,然后离开了

//= require jquery
//= require jquery_ujs
//= require myscripts

i deleted too app/assets/javascripts/rails.js, the file was generated by jquery-rails gem but this is no longer necessary

我删除了app / assets / javascripts / rails.js,该文件是由jquery-rails gem生成的,但不再需要

#3


0  

For me the gotcha was the

对我来说,陷阱是

config.assets.debug = true

option.

选项。

#1


9  

That is because your page is loading jquery_ujs code twice in development mode when precompiled assets exist in /public/assets.

这是因为当/ public / assets中存在预编译资产时,您的页面在开发模式下两次加载jquery_ujs代码。

In development mode javascript requries are loaded with separate tags: jquery, jquery_ujs.js, myscripts.js and finally applications.js. The problem happens when precompiled application.js exists and is used from /public/assets - it contains compilation of all previous files. This is triggered by assets:precompile rake task.

在开发模式下,javascript requries加载了单独的标记:jquery,jquery_ujs.js,myscripts.js,最后是applications.js。当预编译的application.js存在并且从/ public / assets使用时会发生问题 - 它包含所有以前文件的编译。这由资产触发:预编译rake任务。

The solution is to remove /public/assets directory on development then application.js is used (from /app/assets/javascript) which doesn't include previous files. Generally doesn't use assets:precompile rake task on development.

解决方案是在开发时删除/ public / assets目录,然后使用application.js(来自/ app / assets / javascript),它不包含以前的文件。一般不使用资产:预编译rake任务开发。

Update

更新

Adding config.serve_static_assets = false to development.rb also solves problem for me without worrying about /public/assets.

将config.serve_static_assets = false添加到development.rb也为我解决了问题,而不必担心/ public / assets。

#2


5  

A similar thing happened to me upgrading an application from Rails 3.0 to 3.1, it was my mistake. In your

类似的事情发生在我将应用程序从Rails 3.0升级到3.1时,这是我的错误。在你的

app/assets/javascripts/application.js

check that your are not calling twice the rails helpers, i have troubles using

检查你是不是两次铁路助手,我有麻烦使用

//= require_tree .

i have removed this and just left

我删除了这个,然后离开了

//= require jquery
//= require jquery_ujs
//= require myscripts

i deleted too app/assets/javascripts/rails.js, the file was generated by jquery-rails gem but this is no longer necessary

我删除了app / assets / javascripts / rails.js,该文件是由jquery-rails gem生成的,但不再需要

#3


0  

For me the gotcha was the

对我来说,陷阱是

config.assets.debug = true

option.

选项。