title: Rails 4: Passing variables to javascript
body: <p>I've tried numerous methods and followed <a href="http://railscasts.com/episodes/324-passing-data-to-javascript" rel="noreferrer">Ryan Bates'</a> guide but no matter what I do I still get undefined.</p>
  
  <p>application.html.erb</p>
  
  <pre><code>&lt;body&gt;
   &lt;%= content_tag :div, id: 'trackers', data: {trackers: User.count} do %&gt;
   &lt;% end %&gt;
  &lt;/body
  </code></pre>
  
  <p>application.js.erb</p>
  
  <pre><code>var datadump = ($('#trackers').data('trackers'))
  console.log(datadump)
  //returns undefined in console
  </code></pre>
  
  <p>Viewing page source I can see the variable</p>
  
  <pre><code>&lt;div data-trackers="2" id="trackers"&gt;
  </code></pre>
  
  <p>I'm passing User.count for now just to keep it simple but I'll need to be passing @trackers_count which is instantiated in a before_action in the application controller. I should be able to sort that out though once I figure out the problem here. Any suggestions?</p>
  
  <p><strong>UPDATE</strong> - I've simplified all variables down to just trackers, instead of trackers_count to prevent any errors from syntax and updated code here to reflect that.</p>
  
  <p><strong>ANSWER UPDATE</strong> - I selected the correct answer because if you want to pass any variables ASIDE FROM CURRENT_USER those methods worked perfectly. However, you can't access anything with current_user in JS because it loads before the window, so it can't know who the current_user is. It's a real pain but I just did it the long way and accessed the info I need through passing in json and an ajax requests. </p>