title: external javascript file with embedded ruby
body: <p>I'm trying to embed some rails data in an external js file instead of placing it in a script tag in my .html.haml.  How do I do this?  I looked around and read up on .js.erb stuff, but I'm stumped.  I'm also confused about if/when I should be using .rjs at all for this.  I'm using jquery if this helps.  Thanks!</p>
  
  <p>for more info, basically I'm trying to follow along Ryan Bate's railscast <a href="http://asciicasts.com/episodes/223-charts" rel="nofollow">http://asciicasts.com/episodes/223-charts</a>, but put all the graph information in an external js. He mentions that an ajax request should be made when the page is loaded, but I'm not 100% on how to do this.</p>
  
  <p>So far, I've experimented doing the following:</p>
  
  <p>in application.js:</p>
  
  <pre><code>$(document).ready(function(){   
     $.ajaxSetup({  
        'beforeSend': function (xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
     });
     $.ajax({ type: 'POST', url: '/graphs'}); 
  });
  </code></pre>
  
  <p>graphs.js.erb (random stuff to see if it works):</p>
  
  <pre><code>$('.some_container').append('hello!');
  $('h1').append('hello!');
  </code></pre>
  
  <p>this seems to work although the hello's appear after a brief delay, and i'm guessing embedded ruby will work too.  however, if I start adding the graph stuff to this .js.erb it stops working (like <code>data = 1</code>). also it seems like a lot just to have a dynamic js file. </p>
  
  <hr>
  
  <p>Edit: the 'hello's append in all views, so I must be doing something wrong :/ I only wanted it to append to the profile view. </p>