Search code examples
jquerygoogle-chromehyperlinkruby-on-rails-3.1destroy

Location of jquery.js and jquery-ujs.js and why destroy links do not work


I'm a bit confused. In a lot of questions on stackoverflow I read you need jquery-ujs to manage your javascript code, especially if you want these destroy links do work. If I go on this page to follow all the installation instructions I must say I have nothing to do.

Because I'm using Rails 3.1 I have to follow this:

For automated installation in Rails, use the "jquery-rails" gem. Place this in your Gemfile:
gem 'jquery-rails', '>= 1.0.12'
And run: $ bundle install
This next step depends on your version of Rails.

a. For Rails 3.1, add these lines to the top of your app/assets/javascripts/application.js file:

//= require jquery
//= require jquery_ujs

But for Rails 3.1 this is already done after creating a new rails app.

If I run $ rails generate jquery:install I get the following output:

deprecated  You are using Rails 3.1 with the asset pipeline enabled, so this generator is not needed.
            The necessary files are already in your asset pipeline.
            Just add `//= require jquery` and `//= require jquery_ujs` to your app/assets/javascripts/application.js
            If you upgraded your app from Rails 3.0 and still have jquery.js, rails.js, or jquery_ujs.js in your javascripts, be sure to remove them.
            If you do not want the asset pipeline enabled, you may turn it off in application.rb and re-run this generator.

Okay, nothing new. But why my destroy links still doesn't work. So I ask myself the question where are these jquery files located? When I read the jquery installation instruction for another rails version, there they always tell me to copy these files (jquery.js, jquery-ujs.js) to a specific location. Only as a Ruby 3.1 user you don't have to do this. But why? I can't find these files. Are they a part of the jquery-rails gem? If yes, why my destroy links still doesn't work? Is this really an issue of jquery? I mean if I change the helper from link_to to button_to it's working. So how it can be happen as long as it should be an issue of jquery? It might not matter to me, but I don't want to use a form button. Furthermore I can't believe that an application like Rails contains such a big issue in its common generated templates.

So how to fix this without changing the link_to helper and without going away from javascript (Railscast)?

Update 1

  • layout file has <%= javascript_include_tag "application" %>
  • config.assets.enabled = true is set

Update 2

Here the source for the mentioned destroy links:

<%= link_to 'Destroy', @post, confirm: 'Are you sure?', method: :delete %>

Usually it should delete a post, but instead the browser is pointed to the location of that post, e.g. http://domain/posts/2.
If you run this with button_to instead of link_to it will work, but then its a form button.

Update 3

Okay, the destroy links work in Firefox, but I'm a Google Chrome user and I want it to work there as well. Is there a solution for this?

Update 4

Chrome Inspector throw a DOMException on line 5122: var ret = matches.call( node, expr );

message: "SYNTAX_ERR: DOM Exception 12"

But this error occurs on every hyperlink whether it's a destroy link or not.
The destroy link throws an TypeError., but I can't figure out if it comes from jquery.js or from a built in chrome extension. jquery-ujs.js throws the error and the message was "Are you sure?". That's the text defined by rails for a confirm message. So I removed the confirm option and after that the deletion worked.

How to make the confirmation option work in Chrome?


Solution

  • Well, I can confirm that "Better Pop Up Blocker" causes this behaviour.. so I suspect anything else with similar functionality will also cause the same problem.

    On the plus side, with the above plugin you can just whitelist http://localhost and everything starts to works as intended!