1

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>
3
  • you can't use javascript inside of ruby. Ruby is server side. Javascript is client side. To pass such a variable you would need to make an AJAX request. blog.teamtreehouse.com/… Commented Jul 14, 2016 at 3:05
  • When your page loads, Rails generates the HTML, JS, CSS etc then feeds it to the server. Rails code doesn't change the content of the page without reloading the page. If you want your content to change, you're better off finding a way to use your JS to dynamically change the content of your page without calling a Rails function. Commented Jul 14, 2016 at 3:05
  • That's because Server code executes before Client code, therefore the variable is undefined. Use AJAX. Commented Jul 14, 2016 at 4:44

1 Answer 1

2

solved: The right way do to it is to use ajax:

in your page.erb

<% content_for :javascript do %>
    <script>
        $(document).ready(function(){
            $("#category").change(function(){
                var temp_name = $('#category').val();
                $.ajax({
                    url: "/YOUR_PATH/"+temp_name,
                    dataType: "html",
                    type: "GET",
                    data: {ANY_DATA: VALUES},
                    success: function(data){
                        jQuery("#template_overview").html(data);
                    }
                });
            });
        });
    </script>
<% end %>

<div id="template_overview">
</div>

have a partial that populates your div

in the controller:

def display_template
    .
    .
    .
    .
    render partial: '/PATH/template_overview', header: @header
end

and of course the routes

get 'controller/display_template/:temp_id' => 'controller#display_template'
Sign up to request clarification or add additional context in comments.

2 Comments

Don't use quote syntax for something that isn't a quote.
The > markup you used in your question/answer. It is reserved for decorating quotes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.