views:

134

answers:

2

So I'm trying to get some basic jQuery code with Rails working, but it doesn't seem to be working out. I've looked around and it seems like I'm following all of the directions correctly and have tried it in multiple browsers with no avail. Essentially, I'm just trying to slide up a div on document ready, but it just stays there :(

<%= javascript_include_tag ['jquery-1.3.2', 'application'], :cache => true %>
<%= stylesheet_link_tag 'stylesheet' %>
<script type="text/javascript">
$(document).ready(function () {
    $('#login').hide("slide", { direction: "up"}, 5000);
});
</script>
<div id="container">
    <div id="left_nav">
     <p>Core Functions Will Go Here</p>
    </div>
    <div id="headertop"></div>
    <div id="logoheader">
     <%= link_to image_tag("vitaallogo.png"), root_path  %>  
    </div>
    <div id="user_nav">
     <% if current_user%>
      <%= link_to "Logout", logout_path %>
     <% else %>
      <%= link_to "Register", {:controller => 'user', :action => 'new'}%> |
      <%= link_to "Login", login_path %>
     <% end %>
    </div><br />
    <%= yield %> 
</div>
<div id="login">
 <strong>nonononononononono</strong>
</div>

Any information anyone can provide would be appreciated!

A: 

"Not working" is a bit vague. Makes sure the files are correctly included with the javascript_include_tag. AFAIK, when you put a dot in the filename, the helper doesn't automatically append the .js extension. You need to include the full filename.

<%= javascript_include_tag 'jquery-1.3.2.js', 'application', :cache => true %>

Additionally, I would suggest to use jRails.

Simone Carletti
+2  A: 

The issue may be that Rails is using prototype out of the box. There may be a conflict between the two libraries (they both use the $ sign).

Take a look at this: http://docs.jquery.com/Using%5FjQuery%5Fwith%5FOther%5FLibraries

Then, you should try that:

  <script type="text/javascript">
    $j = jQuery.noConflict();

    $j(document).ready(function () {
       $j('#login').hide("slide", { direction: "up"}, 5000);
    });
  </script>

Then if you still want to use prototype, you can still use it using the $ sign.

Guillaume Flandre
You could also not use prototype and go with jRails ( http://github.com/aaronchi/jrails )
marcgg
See the section 'Referencing Magic - Shortcuts for jQuery' (http://docs.jquery.com/Using_jQuery_with_Other_Libraries) for a way to keep $'s meaning for both jQuery and Prototype so you can use all those cool jQuery libraries out there.
Jeff Paquette