Liz
Liz

Reputation: 1437

Using ERB inside Script Tags with jQuery

I'm trying to do some fairly simple jQuery inside script tags on my Rails app (I know, bad developer, no cookie, don't use script tags) and am having trouble with the erb-generated ids. I have the following in my view:

        <% @clients_with_pricing.each do |client| %>
            <h3 id="client-<%= client.id %>-header"><%= client.first_name %> <%= client.last_name %></h3>
            <div id="client-<%= client.id %>-div">
                <h4><%= client.company_name %></h4>
                <ul>
                    <% client.pricings.each do |pricing| %>
                        <li><%= number_to_currency(pricing.price, precision: 0) %>: <%= pricing.product.name %></li>
                    <% end %>
                </ul>
            </div> <!-- client id div -->
        <% end %>

And the following jQuery:

<script type="text/javascript">
    $(document).ready(function() {
        $('.client-<&= client.id %>-header').click(function(){
            $('.client-<&= client.id %>-div').toggleClass('hidden');
        });
    });
</script>

When I look at the rendered HTML the ids get generated properly, but the jQuery still has the erb in it. Can anyone tell me what I'm doing wrong here?

Upvotes: 0

Views: 2069

Answers (2)

Timmy Von Heiss
Timmy Von Heiss

Reputation: 2218

var name = "<%= j @name %>";

Upvotes: 3

Ryenski
Ryenski

Reputation: 9692

The correct syntax for ERB is <% and <%=. Change <&= to <%= and you should be good to go.

Upvotes: 4

Related Questions