Trouble using jQuery in Rails 3.0.7

I’ve tried to install JQuery a couple of times now, and I’ve followed
the steps for Rails UJS mentioned here:

The last time I went ahead and added jQuery UI as well:

$ rails generate jquery:install --ui

  remove  public/javascripts/prototype.js
  remove  public/javascripts/effects.js
  remove  public/javascripts/dragdrop.js
  remove  public/javascripts/controls.js
 copying  jQuery (1.6.1)

identical public/javascripts/jquery.js
identical public/javascripts/jquery.min.js
copying jQuery UI (1.8.12)
create public/javascripts/jquery-ui.js
create public/javascripts/jquery-ui.min.js
copying jQuery UJS adapter (dad698)
remove public/javascripts/rails.js
identical public/javascripts/jquery_ujs.js

As you can see, jQuery was already installed. However, I’m trying to
follow the Getting Started tutorial which ahead a “Hello world” alert
to all hyperlinks on the page, and nothing happens when I click a
link. Here’s what’s in the of my layout:

<%= javascript_include_tag :defaults %>
<%= csrf_meta_tag %>

Firebug is showing that that function is being called on page load,
but when I click this link, it just reloads the page:

My Link

When I view the source, I see the following in the head:

Any ideas as to what I’m missing? I’ve tried both commenting out and
uncommenting this line in application.rb:

config.action_view.javascript_expansions[:defaults] = %w(jquery rails)

Hey,

Not sure if you’ve resolved this issue yet, but you could try replacing:

$(“a”).click(function() {
alert(“Hello world!”);
});

with:

$(function() {
$(“a”).click(function() {
alert(“Hello world!”);
});
});

That’s using the ready() event defined in jQuery, documented here:
http://api.jquery.com/ready/

Hope that helps you out!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs