我想對我的RoR應用程序做一個喜歡/不喜歡的能力。我怎樣才能通過Ajax請求?阿賈克斯請求設置喜歡/不喜歡在軌道上
厭惡和相似 - 都是整數 我怎樣才能讓一個Ajax請求,不是我可以給的什麼,我想在我的方法來增加或者「喜歡」或「不喜歡」計數器數據
我有與職位表:
應用程序/視圖/儀表板/ view.html.erb
<table>
<%if @post.count!=0%>
<%@post.each do |p|%>
<%if !p.text.nil?%>
<tr>
<td><b class="margin"><h4><%=p.text%></b></h4></td>
<td>by <%=p.user.username%> </td>
<td><span class="glyphicon glyphicon-thumbs-up likeAction"><%= link_to p.like, dashboard_like_path, :remote => true, :id => 'likecount' %> </td>
<td><span class="glyphicon glyphicon-thumbs-down"><%= link_to p.dislike, dashboard_dislike_path, :remote => true, :id => 'dislikecount' %> </td>
<%end%>
<% end %>
<%else%>
There's no posts yet, but you can add <%=link_to "one", dashboard_posts_create_a_post_path%>
<%end%>
</table>
我的js文件
種#app/views/dashboard/view.js
$('#likecount').text(@post.like);
$('#dislikecount').text(@post.dislike);
我的方法控制器:
def like
@post.increment!(:like)
respond_to do |format|
format.html
format.js
end
end
def dislike
@post.increment!(:dislike)
respond_to do |format|
format.html
format.js
end
end
我的資產dashboard.js/JavaScript的
jQuery(function($) {
$("likeAction").click(function(){
$.ajax({
url: dashboard_like_path,
type: 'POST',
success: function(){
$('#linkcount').text(data);
}
error: function(error){
alert(error);
}
});
});
});
這個寶石可能對你有用:https://github.com/ryanto/acts_as_votable –