2015-12-18 36 views
0

我想對我的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%>&nbsp;&nbsp; </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); 
     } 
    }); 
    }); 
}); 
+0

這個寶石可能對你有用:https://github.com/ryanto/acts_as_votable –

回答

1

您可以使用Rails的遠程=>真功能,讓AJAX調用。你可以看到更多的細節here

現在回答你的問題,

i。創建鏈接,喜歡不喜歡&像:

<td><span class="glyphicon glyphicon-thumbs-up"><%= link_to p.like, <link-of-like-path>, :remote => true, :id => 'likecount' %> </td> 
<td><span class="glyphicon glyphicon-thumbs-down"><%= link_to p.dislike, <link-of-dislike-path>, :remote => true, :id => 'dislikecount' %></td> 

II。現在在控制器中,您將收到Ajax請求&,您需要以js格式提供響應。所以你喜歡的方法將如下:

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 

iii。創建(action_name).js.erb文件來處理js響應。在你的情況下,它將是app/view/posts文件夾中的like.js.erb文件。這裏寫js代碼來取代like/dislike count。

$('#likecount').text(@post.like); 
$('#dislikecount').text(@post.like); 

如果你想要去與你當前的代碼,那麼你需要在控制器添加JSON處理程序

respond_to do |format| 
    format.html 
    format.json {@post.like} 
end 

,然後用ajax成功更換喜歡的文字,

jQuery(function($) { 
    $("likeAction").click(function(){ 
    $.ajax({ 
     url: path-to-like-function, 
     type: 'POST', 
     success: function(){ 
      $('#linkcount').text(data); 
     } 
     error: function(error){ 
      alert(error); 
     } 
    }); 
    }); 
}); 
+0

我更新了它,你可以看一下嗎? – VPaskar

+0

您的js文件需要與您的操作名稱一致。所以對於像'app/views/dashboards/like.js.erb'和'app/views/dashboards/dislike.js.erb'。由於我們以js格式發送請求,因此您需要一些js格式的響應處理程序。默認情況下,它會查找與動作名稱相同的文件。如果要渲染不同的js文件,請在'format.js'塊中提及該文件名 – bunty