title: use javascript variable inside <% %> in erb - Ruby on Rails
body: <p>I am using <code>select_tag</code> and <code>onchange</code>. I need to call a helper method and pass the value of the selected item as a parameter:</p>
  
  <p>This is how my helper method looks like</p>
  
  <pre><code>def find_template(temp)
      "Hello #{temp}!"
  end
  </code></pre>
  
  <p>What I need to do is to get user's selection, using javascript, and pass it to <code>find_template</code>. If I pass a string instead of template, it works fine, i.e. <code>&lt;% @header = find_template("option_1") %&gt;</code> but when I use <code>var template</code> I get the following error:</p>
  
  <pre><code>undefined local variable or method `template' for ...
  </code></pre>
  
  <p>and this is my erb</p>
  
  <pre><code>&lt;% content_for :javascript do %&gt;
      &lt;script&gt;
          function template() {
              var e = document.getElementById("category");
              var template = e.options[e.selectedIndex].text;
              &lt;% @header = find_template(template) %&gt;
          }
      &lt;/script&gt;
  &lt;% end %&gt;
  
  &lt;%= select_tag(:category, options_for_select(
                   Blah::TYPES.map {|k, v| [v['name'], k] }),
                   {onChange: 'template()'}) %&gt;
  
  &lt;div id="template-overview"&gt;
      &lt;table&gt;
          &lt;tr&gt;
              &lt;% @header.each do |column| %&gt;
                  &lt;th class="&lt;%= cycle('even', 'odd') -%&gt;"&gt;
                      &lt;%= column %&gt;
                  &lt;/th&gt;
              &lt;% end %&gt;
          &lt;/tr&gt;
      &lt;/table&gt;
  &lt;/div&gt;
  </code></pre>