为什么Rails的UJS AJAX:成功绑定被调用两次? [英] Why is Rails UJS ajax:success bind being called twice?
问题描述
我有一个简单的形式:
= form_for(posts_path, :id => "new_post", :remote => true) do
= text_field_tag "post[input]"
= submit_tag "Post!"
我已绑定一个回调到 AJAX:成功
事件:
$("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?
我使用Rails 3.1的默认情况下是使用jquery-UJS。
I'm using Rails 3.1 which by default is using jquery-ujs.
推荐答案
那是因为你的页面加载 jquery_ujs
code在开发模式下的两倍时,$ P $在 /公/资产
。
That is because your page is loading jquery_ujs
code twice in development mode when precompiled assets exist in /public/assets
.
在JavaScript的requries装载有独立标签的发展模式:的jQuery
, jquery_ujs.js
, myscripts.js
最后 applications.js
。问题发生时,precompiled 的application.js
的存在,并从 /公/资产
使用 - 它包含编译所有previous文件。这是触发资产:precompile
rake任务
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.
解决的办法是删除 /公/资产
对开发目录中,然后的application.js
终止(从使用 / APP /资产/ JavaScript的
),这还不包括previous文件。
一般不使用资产:发展precompile
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.
更新
添加 config.serve_static_assets = FALSE
到 development.rb
也解决了问题,对我来说,无需担心 /公/资产
。
Adding config.serve_static_assets = false
to development.rb
also solves problem for me without worrying about /public/assets
.
这篇关于为什么Rails的UJS AJAX:成功绑定被调用两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!