I’m trying to add some jQuery + ERB to a specific view:
views/posts/show.html.erb (top of the file):
<% content_for :javascript do %>
<script type="text/javascript">
$(".post-<%=@post.id%> h3").prepend('<%=escape_javascript @post.votes.count %>');
</script>
<% end %>
<h2>posts show</h2>
(etc...)
<div class="post-<%=@post.id%>">
<h3>votes</h3><br />
<%= link_to "Vote Up", vote_up_path(@post), :remote => true %><br />
</div>
views/layouts/application.html.erb (bottom of the file):
(etc...)
</div>
<%= yield %>
<%= yield :javascript %>
</body>
</html>
But I’m getting the following error:
undefined method `gsub' for 6:Fixnum
Extracted source (around line #3):
1: <% content_for :javascript do %>
2: <script type="text/javascript">
3: $("post-<%=@post.id%>").html('<%=escape_javascript @post.votes.count %>');
4: </script>
5: <% end %>
Any suggestions to fix this?
escape_javascriptcallsgsubon whatever you pass it, which doesn’t make sense for a number. You can either not callescape_javascriptor give it a String instead: