title: Accessing Ruby objects in javascript
body: <p>I'm working on a rails app (4.1.8) that requires a lot of javascript in my show view, and I need a way to access data returned from ActiveRecord within the actual js file</p>
  
  <p>Here's an example:</p>
  
  <p>My Show controller</p>
  
  <pre><code>def show
    @user = User.find(params[:id])
  end
  </code></pre>
  
  <p>now i need to access this data in my show javascript, but this is the only way i could do it:</p>
  
  <p>My Show view</p>
  
  <pre><code>&lt;%= javascript_tag do %&gt;
      var myEmail = "&lt;%= j @user.email %&gt;";
      // do stuff with myEmail
  &lt;% end %&gt;
  </code></pre>
  
  <p>My question is, is there a way I can render this javascript or include it in a separate script tag while maintaining the userdata? I don't want to have to write hundreds of lines of jQuery and js inside this javascript_tag and it's painful to debug. Everything I've tried so far (putting it in the asset pipeline, rendering a seperate .js.erb file) just turns myEmail into a string with the ruby statement inside it. Any ideas? Thanks in advance!</p>