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:
- 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 => { :confirm => "Are you sure you want to delete this comment?" }
- if likes.count > 0
.comment-likes
- likers = likes.map { |like| link_to(like.user_name, "#") }
- if likers.length > 1
- likers = likers.slice(0, likers.length - 1).join(", ").concat(" and " + likers.slice(-1))
- else
- likers = likers[0]
= "Liked by #{likers}".html_safe
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.
Heres my comments_controller.rb stuff
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
= link_to "Like", like_comment_path(comment), method: :post, remote: trueThen in your likes_controller create action, respond_to format.js
This will then invoke the create.js.erb in your view folder, and you can dynamically update your html from there