1

I am setting up a jquery+rails application. There seems to be no problem with including jquery. The source file finally generated finally looks like --->

<!DOCTYPE html>
<html>
    <head>
        <title>CodeAliker</title>
        <link href="/assets/application.css?body=1" media="screen" rel="stylesheet" type="text/css" />
<link href="/assets/bootstrap.css?body=1" media="screen" rel="stylesheet" type="text/css" />
        <script src="/assets/jquery.js?body=1" type="text/javascript"></script>
<script src="/assets/jquery_ujs.js?body=1" type="text/javascript"></script>
<script src="/assets/application.js?body=1" type="text/javascript"></script>
        <meta content="authenticity_token" name="csrf-param" />
<meta content="EIklYq2jXM/BSsN+M0V5x9GoFA+WjwYuD0kuLBkjIUg=" name="csrf-token" />
    </head>

    <body>

        <script src="/assets/dashboard.js?body=1" type="text/javascript"></script>
<link href="/assets/dashboard.css?body=1" media="screen" rel="stylesheet" type="text/css" />

<div class="topbar">
    <div class="fill">
          <div class="container">
            <a class="brand" href="#">CodeAliker</a>
            <ul class="nav">
                <li class="active" name='useritem'>
                    <a href="#user" >+myth</a>
                </li>
                <li name='dashitem'>
                    <a href="#panel">Dashboard</a>
                </li>
                <li>
                    <a href="/about.html">About</a>
                </li>
                <li class="nav secondary">
                    <a href="/getout">SignOut</a>
                <li>
            </ul>
          </div>
    </div>
</div>

<div id="bodydiv">
  <div class="container-fluid">
    <div class="sidebar">
              <table class="bordered-table">
                <thead>
                  <tr>
                    <th>BATCH List</th> 
                  </tr>
                </thead>                        
                <tbody id='table-body'>
                  <tr>
                    <td><a href="#">samplebatch</a></th>
                  </tr>
                </tbody>    
            </table>
    </div>

    <div class="content">
               ...
    </div>
  </div>
</div>


    </body>
</html>

My Jquery code in a file dashboard.js looks like -->

$('ul.nav li').each(function(){
    console.log('Iterating');
});

$('a').bind('click', function() {  
    alert('Unobtrusive!');  
});  

The code never executes ,why? However inserting a simple console.log() on the dashscript.js prints!

8
  • Why did you put one script and link in body? Put everything in the head only. And try. Commented Feb 7, 2012 at 8:03
  • It is controller specific scripts and not application wide scripts so I had included them in my 'dashboard.html.erb' file. The generated code has not put it in head. :| Commented Feb 7, 2012 at 8:14
  • Did dashboard.js is getting downloaded? You can check in fiddler. Also you have put query string for refreshing the script, try incrementing it to body=2. You might have updated the script and your browser has cached old version. Commented Feb 7, 2012 at 8:18
  • Anyways I fixed this issue by including controller_name in the application.html.erb but it is still not working! :| Commented Feb 7, 2012 at 8:23
  • 1
    let us continue this discussion in chat Commented Feb 7, 2012 at 8:30

1 Answer 1

2

I have put your code in jsFiddle.net and it works : http://jsfiddle.net/kskHX/2/

There might be issue like

  • Your dashboard.js is not getting downloaded, might be the url is incorrect or invalid. Check in fiddler, firebug, or console of chrome for errors.
  • Your browser has cached the js file, and the updated file is not getting downloaded. Change your query string for the js and try again.

Solution

The dashboard.js was getting downloaded but the functions were not getting executed , asked to shift code to $(document).ready( and it worked.

Hope this info helps you.

Sign up to request clarification or add additional context in comments.

2 Comments

Its something else, the dashboard.js is getting downloaded. Also I cleared browser cache and tried it all over again. :|
Yup! Works flawless now. Thank you for the help! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.