简体   繁体   中英

Rails 3.1 with Asset Pipeline, link_to :confirm message showing twice?

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)

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' 

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

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

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.

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.

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.

This was happening to me too. Removing "//= require_tree." from application.js fixed it.

Mention the name of jquery and jquery_ujs with version in application.js if still not workout then take compressed version of jquery and jquery_ujs.

or

Add gem 'jquery-rails' into your gemfile then try out.

Probably you have the rails.js file. Remove it and try it again. It must work.

FoN

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM