【问题标题】:Ajax to implement like function on a blogAjax 在博客上实现类似功能
【发布时间】:2012-10-31 01:01:26
【问题描述】:

问题:Ajax 未按应有的方式运行。

邮政索引:

<tr>
  <td><%= post.name %></td>
  <td><%= post.created_at.strftime("%Y/%m/%d, %I:%M%p") %></td>
  <td><%= post.view %></td>
  <td id="post_like_<%= post.id %>"><%= post.like %></td>
  <td><%= link_to 'like', like_post_path(post), :remote => true %></td>
</tr>

like.js.erb:

$("#post_like_<%= post.id %>").html("<%= @post.like %>");

我只有在刷新页面后才能看到更改。 Ajax 无法正常工作,但我未能在上述代码中找到问题所在。

【问题讨论】:

    标签: ruby-on-rails ajax html-table


    【解决方案1】:

    您在视图中忘记了@。我看到$post 在实例变量中,正如我看到的$post.like。因此,在您的like.js.erb 中,将post.id 替换为@post.id。喜欢:

    $("#post_like_<%= @post.id %>").html("<%= @post.like %>");
    

    【讨论】:

    • 很棒的发现!我不敢相信我花了几个小时没有注意到这个小问题。
    猜你喜欢
    • 1970-01-01
    • 2014-04-15
    • 2019-06-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-28
    • 1970-01-01
    • 2016-11-20
    相关资源
    最近更新 更多