title: ajax in ruby on rails for a like/unlike button
body: <p>So i have a page with a like button under my comments and am trying to work out how to do this without refreshing the whole page currently the code looks like this:</p>
  
  <pre><code>- if can? :like, comment
    = " · "
    - if likes.find_by_user_id(current_user.id).nil?
      = link_to "Like", like_comment_path(comment), method: :post
    - else
      = link_to "Unlike", unlike_comment_path(comment), method: :post
  - if comment.user == current_user
    = " · "
    = link_to "Delete", comment_path(comment), method: :delete,
      :data =&gt; { :confirm =&gt; "Are you sure you want to delete this comment?" }
  - if likes.count &gt; 0
  .comment-likes
    - likers = likes.map { |like| link_to(like.user_name, "#") }
    - if likers.length &gt; 1
      - likers = likers.slice(0, likers.length - 1).join(", ").concat(" and " +                               likers.slice(-1))
    - else
      - likers = likers[0]
    = "Liked by #{likers}".html_safe
  </code></pre>
  
  <p>Changing the method for like and unlike :post to :update seemed to make a big difference is this function using ajax and :post not? If not then how would i go about making this into a ajax function.</p>
  
  <p>Heres my comments_controller.rb stuff</p>
  
  <pre><code>def like
   comment_vote = resource.like current_user
   Event.comment_liked!(comment_vote)
   redirect_to discussion_url(resource.discussion)
  end
  
  def unlike
   resource.unlike current_user
   redirect_to discussion_url(resource.discussion)
  end
  </code></pre>