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

雨燕双飞 提交于 2020-01-01 04:07:05

问题


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.


回答1:


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.




回答2:


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




回答3:


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.




回答4:


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

FoN



来源:https://stackoverflow.com/questions/7084087/rails-3-1-with-asset-pipeline-link-to-confirm-message-showing-twice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!