【问题标题】:Rails: Two different Ajax features not working together on same buttonRails:两个不同的 Ajax 功能不能在同一个按钮上协同工作
【发布时间】:2012-02-29 19:52:43
【问题描述】:

制作了一个通过 JS 文件显示/隐藏的评论系统,但同时该评论系统由用作查看计数器的同一按钮控制。两者都由两个不同的 Ajax 文件运行,但如果另一个文件不工作,两者都将无法工作。任何帮助都感激不尽!谢谢!

EDIT HTML 没有使用@micropostmicropost,因为@micropost 是在不同的部分中定义的。对于那些熟悉 Michael Hartl 的 Rails 教程的人,我用他的代码和教义做了这篇微博。我知道这搞砸了 ajax 的一切,它带回了 @micropost 而不是 micropost 但它不会让我不在 ajax 文件中放 @ 所以我迷路了

评论按钮 HTML

<span class='count-<%= micropost.id%>'>
<a href="/microposts/<%=micropost.id %>/upview" data-remote='true' class='CommentTitle' data-micropost='<%= micropost.id %>'>Comments</a>
</span>

评论 Ajax 以改变计数器

$("#count-<%=@micropost.id%>").html('<%="#{@micropost.view_count}"%>');
$(".count-<%=@micropost.id%>").html('<a href="/microposts/<%=@micropost.id%>/upview" data-remote="true" class="CommentTitle" data-micropost="<%= @micropost.id %>">Comments</a>');

评论按钮滑动切换 JS

$(".CommentTitle").click(function(){
var title = this;
var postID = $(this).data('micropost');
$("#CommentContainer-" + postID).slideToggle("slow", function(){ 
$(".CommentTitle", title).html($(this).is(":hidden") ? "Comments" : "Comments");  
}); 
});

【问题讨论】:

    标签: ruby-on-rails ajax ruby-on-rails-3 jquery


    【解决方案1】:

    假设:当用户点击 html 中的链接时,他们正在向您的 upview 操作发送请求到您的 MicropostsController

    在该控制器操作方法中,您可以分配一个实例变量...

    @this_micropost = Micropost.find(params[:id]) # assuming you're RESTful, which it looks like
    

    然后,在您的upview.js.erb 中,您可以将该实例变量用于您想要的任何东西。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多