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