Topic: jQuery not defined in rails app

In rails app, I add the following line of code in application.js and I get jQuery is not defined in firebug console.
In application.html.erb, I have the following:
<script type="text/javascript" src="../javascripts/application.js"></script>
<script type="text/javascript" src="../javascripts/jquery-1.4.2.js"></script>

and I installed jquery in public/javascripts


Any ideas what's going wrong? thanks for reply.

(function($) {
   
    var linksToInt = {
        "#pple": 0,
        "#serv": 1,
        "#sol": 2,
        "#dash": 3,
        "#adm": 4,
        "#inv": 5,
        "#stu": 6
       
    }
   
    /*
    links = document.getElementsByTagName("a");
    if(links.className.indexOf("div-link") > -1){
        links.onclick = function{ removeHash(this.getAttribute("href"));};
    }
    */
   
   
    $("a.div-link").click(function(){displayDiv($(this).attr("href"));});
   
    function displayDiv(id){
    var linkInt = linksToInt[id];
    on_btn_click(linkInt);
    }
   
    function on_btn_click(displayDiv){
        displayDiv != null ? null : this;
       
        switch(displayDiv){
            case 0:
                $(content).empty();
                $(displayDiv).show();
                break;
            case 1:
                $(content).empty();
                $(displayDiv).show();
                break;
            case 2:   
                $(content).empty();
                $(displayDiv).show();
                break;
            case 3:
                $(content).empty();
                $(displayDiv).show();
                break;
            case 4:
                $(content).empty();
                $(displayDiv).show();
                break;
            case 5:
                $(content).empty();
                $(displayDiv).show();
                break;   
            case 6:
                $(content).empty();
                $(displayDiv).show();
                break;
        }
    }

})(jQuery);

Last edited by johnmerlino (2010-06-16 12:57:51)

Re: jQuery not defined in rails app

a wise man once said 'In order to use jquery and rails,  you have to replace the prototype helpers with jquery helpers, essentially.'

I believe jQuery and prototype screw each other up. check out this post for info.

Last edited by RailsRhino (2010-06-16 13:27:04)

- Ben

Re: jQuery not defined in rails app

Actually stepcarousel.js which uses jquery is working in the application
so I don't think there is a conflict with prototype. However I still get
the error jquery is undefined.

Re: jQuery not defined in rails app

I've not very good with JavaScript,  but I've never seen syntax quite the way you've shown it.  My application.js files always look something like this:

$(document).ready(function()
{
 
    formatPercent = function(num) {
        return ('% ' + num.toString().replace(/[^0-9]/g,''));
    }
});

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: jQuery not defined in rails app

WAIT!!!

could it be your application.js is loaded BEFOR jQuery??

switch them around!

Joe got a job, on the day shift, at the Utility Muffin Research Kitchen, arrogantly twisting the sterile canvas snout of a fully charged icing anointment utensil.

Re: jQuery not defined in rails app

Great hint, Brad. I've been strugling with a similar problem for hours, trying to implement jqgrid. I just switched the sequence of these  lines in the application.html.erb file, and it worked. Thanks! From:        <%= jqgrid_javascripts %>    <%= jqgrid_stylesheets %>        <%= stylesheet_link_tag