我使用行爲作爲投票寶石,並在評論中實現了投票系統。現在,我希望頁面在用戶點擊upvote或downvote鏈接並更新投票計數器時停止重新加載。我捆綁使用此使用xmlhttprequest更新div與ajax onclick導軌
<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 %>
我無法弄清楚什麼網址我應該在這條線
xmlhttp.open("GET","URL_HERE",true);
任何人都可以使用告訴我如何做到這一點。
有你必須使用XHR還是可以使用'jQuery'('$ .ajax')? –
任何可以工作的東西都適合我......我甚至使用rackcasts視頻的機架pjax ...但無法使其工作 – Shuvro
好的謝謝 - 讓我在幾分鐘內寫出答案 –