【发布时间】:2014-01-25 09:33:24
【问题描述】:
我将行为用作可投票的 gem,并在 cmets 中实现了投票系统。现在我希望页面在用户单击 upvote 或 downvote 链接并更新投票计数器时停止重新加载。我想用这个
<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","URL_HERE",true);
xmlhttp.send();
}
</script>
我的_comment.html.erb
<%= div_for comment do %>
<p>
<div style="float: left; text-align: center; margin-right: 20px;" class="comment">
<% if user_signed_in? %>
<% if current_user.voted_for? comment %>
<%= link_to image_tag("updis.png", {:style => 'width: 30px'}), like_post_comment_path(@post, comment), method: :put, :disabled => 'disabled' %><br />
<% if comment.likes.size > comment.dislikes.size %>
+<%= comment.likes.size-comment.dislikes.size %><br />
<% elsif comment.likes.size < comment.dislikes.size %>
<%= comment.likes.size-comment.dislikes.size %><br />
<% else %>
<%= comment.likes.size-comment.dislikes.size %><br />
<% end %>
<%= link_to image_tag("downdis.png", {:style => 'width: 30px'}), dislike_post_comment_path(@post, comment), method: :put, :disabled => 'disabled' %>
<% else %>
<%= link_to image_tag("upvote.png", {:style => 'width: 30px'}), like_post_comment_path(@post, comment), method: :put %><br />
<% if comment.likes.size > comment.dislikes.size %>
+<%= comment.likes.size-comment.dislikes.size %><br />
<% elsif comment.likes.size < comment.dislikes.size %>
<%= comment.likes.size-comment.dislikes.size %><br />
<% else %>
<%= comment.likes.size-comment.dislikes.size %><br />
<% end %>
<%= link_to image_tag("downvote.png", {:style => 'width: 30px'}), dislike_post_comment_path(@post, comment), method: :put %>
<% end %>
<% else %>
<%= link_to image_tag("updis.png", {:style => 'width: 30px'}), like_post_comment_path(@post, comment), method: :put, :disabled => 'disabled' %><br />
<% if comment.likes.size > comment.dislikes.size %>
+<%= comment.likes.size-comment.dislikes.size %><br />
<% elsif comment.likes.size < comment.dislikes.size || comment.votes.size == 0 %>
<%= comment.likes.size-comment.dislikes.size %><br />
<% else %>
<%= comment.likes.size-comment.dislikes.size %><br />
<% end %>
<%= link_to image_tag("downdis.png", {:style => 'width: 30px'}), dislike_post_comment_path(@post, comment), method: :put, :disabled => 'disabled' %>
<% end %>
</div>
<div style="float: left; margin-right: 20px;">
<%= image_tag avatar_url(comment.user), class: 'profile-picture' %>
</div>
<strong>
<%= time_ago_in_words(comment.created_at).capitalize %> আগে <%= link_to comment.user.name, comment.user %> বলেছেন,
</strong>
<p>
<%= comment.body %><br/><br />
</p>
</p>
<hr />
<% end %>
我不知道应该在这一行中使用什么 URL
xmlhttp.open("GET","URL_HERE",true);
谁能告诉我怎么做。
【问题讨论】:
-
你必须使用 XHR 还是可以使用
jQuery($.ajax) ? -
任何对我有用的东西...我什至使用 railscasts 视频的机架 pjax...但无法使其工作
-
好的,谢谢 - 让我在几分钟后写答案
-
不使用 Rails 的内置 ajax 工具有什么特殊原因吗? edgeguides.rubyonrails.org/…
-
@scaryguy 你能告诉我我必须用什么来代替 appendTo 来简单地更新 cmets div 并更新投票数吗? appendTo 在旧评论下添加相同的评论并更新投票......两者同时存在......
标签: javascript ruby-on-rails ajax