16

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.

Community
  • 1
  • 1
Ben Kreeger
  • 6,355
  • 2
  • 38
  • 53

4 Answers4

25

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.

Mattias Wadman
  • 11,172
  • 2
  • 42
  • 57
  • 1
    I'm using Rails 3.0.10 and it happened to me after installing the jquery-rails gem and running its generator. Rails.js hadn't been removed, and removing that fixed mine. – PhillipKregg Feb 11 '12 at 00:58
  • 1
    It's about time I accepted this as an answer — this worked best for me. Also note that running `bundle exec rake assets:clean` will work for removing the `public/assets` directory. Most of this all was because I still didn't quite write up and store my stylesheets in the least-friction-manner that the Asset Pipeline wants you to. That's what I get for trying to be different, I guess… – Ben Kreeger Feb 23 '12 at 18:44
  • Thanks. Didn't know about `assets:clean`, good to know. Seams to do [more or less the same thing](https://github.com/rails/rails/blob/master/actionpack/lib/sprockets/assets.rake#L83) but seams more correct. – Mattias Wadman Feb 23 '12 at 22:46
4

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

kidcapital
  • 5,064
  • 9
  • 46
  • 68
  • This may cause to you as you are including a same file twice one manually by **javascript_include_tag** and other by application.js. First confirm that if this is the case you have to include it once. – Taimoor Changaiz May 21 '13 at 03:04
0

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.

Rameshwar Vyevhare
  • 2,699
  • 2
  • 28
  • 34
  • The `jquery-rails` gem was already in my Gemfile (as stated in my question). Removing `jquery` & `jquery_ujs` completely removed all Rails/jQuery view behavior. – Ben Kreeger Feb 23 '12 at 18:46
-1

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

FoN

Peter DeWeese
  • 18,141
  • 8
  • 79
  • 101
  • I don't have a `rails.js` file anywhere in my project; not in `app/assets/javascripts`, not in `public/javascripts`, not anywhere. There's also no references or broken links to any `rails.js` file. – Ben Kreeger Oct 05 '11 at 15:45