dimanche 20 décembre 2015

Getting an error with ajax-request

i wanted to make a like/dislike ability on my web application. But I get errors here : 1) Like/dislike increments by 2 per ONE click 2) The request is sended, it updates the like/dislike variable, but i have to reload the page to see the new updated data What am i doing wrong ? Here's my code :

my view

#app/views/dashboard/view.html.erb 
<td><span class="glyphicon glyphicon-thumbs-up likeAction"><%=  p.like %> </td>
<td><span class="glyphicon glyphicon-thumbs-down dislikeAction"><%= p.dislike %> </td>

my controller

#app/controllers/dashboard_controller.rb 
  def like
    @post=Post.find(params[:id])
    @post.increment!(:like)
  end

  def dislike
    @post=Post.find(params[:id])
    @post.increment!(:dislike)
  end

my js file

jQuery(function($) {
  $(".likeAction").click(function(){
    var current_post_tr = $(this).parents('tr')[0];
    $.ajax({
      url: 'http://localhost:3000/dashboard/' + $(current_post_tr).attr('data-post_id') +'/like',
        type: 'PUT',
        success: function(){
          $("#likeAction").hide().fadeIn();
        }
     });
  });

  $(".dislikeAction").click(function(){
    var current_post_tr = $(this).parents('tr')[0];
    $.ajax({
      url: 'http://localhost:3000/dashboard/' + $(current_post_tr).attr('data-post_id') +'/dislike',
        type: 'PUT',
        success: function(){
          $("dislikeAction").hide().fadeOut();
        }
    });
  });
});

So it doesn't hide and then doesn't fade In

Aucun commentaire:

Enregistrer un commentaire