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 <head> of my layout:

<%= javascript_include_tag :defaults %> <%= csrf_meta_tag %>     <script type="text/javascript" charset="utf-8">       $("a").click(function() {                 alert("Hello world!");             });     </script>

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

<a href="">My Link</a>

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

<script src="/javascripts/jquery.js?1306339461" type="text/ javascript"></script> <script src="/javascripts/jquery-ui.js?1306344213" type="text/ javascript"></script> <script src="/javascripts/jquery_ujs.js?1306339461" type="text/ javascript"></script> <meta name="csrf-param" content="authenticity_token"/> <meta name="csrf-token" content="kCLegMZ7rgxMMlXnha5DQ2/8A6hMR9mEBX0XWFJFGSw="/> <script type="text/javascript" charset="utf-8"> $("a").click(function() { alert("Hello world!"); }); </script>

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)


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

$(“a”).click(function() {

alert(“Hello world!”);



$(function() { $(“a”).click(function() {

alert(“Hello world!”);



That’s using the ready() event defined in jQuery, documented here:

Hope that helps you out!