title: Using Rails' view helpers inside javascript templates
body: <p>I'd like to be able to use some of rails' view and form helpers, such as <code>&lt;%= image_tag .. %&gt;</code> or <code>&lt;%= select_tag .. %&gt;</code> inside my javascript templates. I read <a href="https://stackoverflow.com/a/8317266/187907">this thread</a> that suggested pre-rendering the erb as a string in some javascript variable and then calling it from the template (it'll have to be available before the js template is called). So I could do something like this:</p>
  
  <h3>view_helpers.js.erb</h3>
  
  <pre><code>&lt;script type="text/javascript"&gt;
    var ViewHelpers = {
        CountrySelect: "&lt;%= select_tag "person[country_id]", options_from_collection_for_select(Country.all, "name", "id"), :prompt =&gt; '-Country-' %&gt;"
    };
  &lt;/script&gt;
  </code></pre>
  
  <p>And then call it from my eco template (rendered later):</p>
  
  <h3>edit_person.jst.eco</h3>
  
  <pre><code>...
  &lt;p&gt;
    &lt;label for="person_country_id"&gt;Country&lt;/label&gt;
    &lt;%= ViewHelpers.CountrySelect %&gt;
  &lt;/p&gt;
  ...
  </code></pre>
  
  <p>However, I seem to be unable to load it both as part of the view or using the asset pipeline:</p>
  
  <h3>Asset Pipeline: application.js</h3>
  
  <p>(saved in app/assets/javascripts/views/people/view_helpers.js.erb)</p>
  
  <pre><code>//= require ./views/people/view_helpers
  </code></pre>
  
  <h2>OR</h2>
  
  <h3>ERB template: views/people/index.html.erb</h3>
  
  <p>(saved in app/views/people/view_helpers.js.erb)</p>
  
  <pre><code>&lt;%= render :template =&gt; 'people/view_helpers' %&gt;
  </code></pre>
  
  <p>Am I approaching this problem completely wrong or have I missed something? thanks.</p>