Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 553
Ben Kreeger Avatar asked Aug 16 '11 19:08

Ben Kreeger


2 Answers

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.

like image 68
Mattias Wadman Avatar answered Nov 19 '22 23:11

Mattias Wadman


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

like image 4
kidcapital Avatar answered Nov 20 '22 00:11

kidcapital