使用 jQuery + ERB 在视图中获取 6:Fixnum (Rails) 的未定义方法“gsub"? [英] Getting undefined method `gsub' for 6:Fixnum (Rails) in a view with jQuery + ERB?

查看:31
本文介绍了使用 jQuery + ERB 在视图中获取 6:Fixnum (Rails) 的未定义方法“gsub"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向特定视图添加一些 jQuery + ERB:

views/posts/show.html.erb(文件顶部):

<% content_for :javascript do %><script type="text/javascript">$(".post-<%=@post.id%> h3").prepend('<%=escape_javascript @post.votes.count %>');<%结束%><h2>帖子显示</h2>(等等...)<div class="post-<%=@post.id%>"><h3>投票</h3><br/><%= link_to "Vote Up", vote_up_path(@post), :remote =>真%><br/>

views/layouts/application.html.erb(文件底部):

(等等...)

<%=产率%><%=产量:javascript%></html>

但我收到以下错误:

 6:Fixnum 的未定义方法 `gsub'提取的源代码(围绕第 3 行):1:<% content_for :javascript do %>2:<script type="text/javascript">3: $("post-<%=@post.id%>").html('<%=escape_javascript @post.votes.count %>');4:</脚本>5:<%结束%>

有什么建议可以解决这个问题吗?

解决方案

escape_javascript 调用 gsub 传递给它的任何内容,这对数字没有意义.你可以不调用 escape_javascript 或者给它一个字符串:

$("post-<%=@post.id%>").html('<%=escape_javascript @post.votes.count.to_s %>');

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_javascript calls gsub on whatever you pass it, which doesn't make sense for a number. You can either not call escape_javascript or give it a String instead:

$("post-<%=@post.id%>").html('<%=escape_javascript @post.votes.count.to_s %>');

这篇关于使用 jQuery + ERB 在视图中获取 6:Fixnum (Rails) 的未定义方法“gsub"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆