本文介绍了带有Asset Pipeline的Rails 3.1,link_to:confirm消息显示两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我已经看到关于这个问题的这个问题是由jQuery或Prototype的多次链接引起的,但我可以确认我只在整个页面上链接到jQuery 一次.我的问题是:当我有一个确认删除的link_to时,弹出窗口显示两次.这是模板中的适用代码(用Slim编写):

Okay, so I've seen this question about this problem being caused by multiple linkings of jQuery or Prototype, but I can confirm that I'm only linking to jQuery once on the entire page. My problem is this: when I have a link_to that confirms a deletion, the popup shows twice. Here's the applicable code in my template (written in Slim):

link_to('Destroy', depot_path(@depot.id), :confirm => "Really?", :method => :delete)

我正在运行 Rails 3.1.0 ,并且已打开资产管道,并且Gemfile中有gem 'jquery-rails',而以下内容位于我的application.js文件中(由资产管道链轮.)

I'm running Rails 3.1.0 with the Asset Pipeline turned on, with gem 'jquery-rails' in my Gemfile, and the following is in my application.js file (which is compiled by Sprockets for the asset pipeline).

//= require jquery
//= require jquery_ujs
//= require 'underscore'
//= require 'backbone'

我的/vendor/assets/javascripts/目录中有underscore.jsbackbone.js,链轮似乎还可以.我还搜索了链轮服务的application.js文件,jQuery仅存在一次,而jQuery UJS仅存在一次.这就是我的页面渲染时的样子(我省略了显示的csrf-token值FWIW).

I have underscore.js and backbone.js in my /vendor/assets/javascripts/ directory, and sprockets seems to find those okay. I've also searched through the application.js file that sprockets serves up, and jQuery is only in there once, and jQuery UJS is only in there once. This is what my head looks like when my page renders (I've omitted the csrf-token value for display, FWIW).

<head>
  <meta content="text/html; charset=utf-8" http-equiv="content-type">
  <title>Administration</title>
  <link href="/assets/screen.css" media="screen" rel="stylesheet" type="text/css" />
  <script src="/assets/application.js" type="text/javascript"></script>
  <meta content="authenticity_token" name="csrf-param" />
  <meta content="--token--omitted--" name="csrf-token" />
  <script src="/assets/common/subdata.js" type="text/javascript"></script>
  <link href="/assets/show.css" media="screen" rel="stylesheet" type="text/css" />
</head>

subdata.js中包含一些特定于Backbone的代码;没有什么可以选择再次包含jQuery的.那怎么办?我的项目中的任何地方都没有其他jQuery文件;全部通过jquery-rails gem进行管理.是什么导致我的:confirm方法触发两次?

subdata.js has some Backbone-specific code in it; nothing that would choose to include jQuery again. So what's the deal? I don't have an additional jQuery file anywhere in my project; it's all managed through the jquery-rails gem. What's causing my :confirm method to fire twice?

编辑:我以前在Rails 3.1的RC5上看到过这个,但现在我也在Rails 3.1的实际上看到了.

EDIT: I was previously seeing this on RC5 of Rails 3.1, but now I'm also seeing it on Rails 3.1 actual.

推荐答案

这发生在我身上,因为我在开发环境中运行了rake assets:precompile,导致创建了public/assets/application.js.这就要求由该静态文件满足/assets/application.js的请求,该静态文件包含一起编译的public/assets/application.js中的所有// require脚本,从而使它们再次被加载.

This has happened to me because I had run rake assets:precompile in my development environment causing public/assets/application.js to be created. This makes requests for /assets/application.js to be served by this static file which contains all // require scripts in public/assets/application.js compiled together, causing them to be loaded once again.

在开发模式下,<%= javascript_include_tag "application" %>将扩展为多个<script>标记,一个标记用于// require行所需的每个文件,另一个标记用于application.js仅包含其自身内容的

In development mode <%= javascript_include_tag "application" %> will expand in to multiple <script> tags, one for each file required by // require lines, and also one for application.js which only contain its own content.

解决方案是手动删除整个public/assets目录或使用assets:clean rake任务.这将导致脚本文件再次动态提供.

Solution is to remove the whole public/assets directory manually or use the assets:clean rake task. This will cause scripts files to be served dynamically again.

这篇关于带有Asset Pipeline的Rails 3.1,link_to:confirm消息显示两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 15:08