Reimplement ajax like/unlike behaviour

This commit is contained in:
Daniel O'Connor
2016-07-09 21:53:33 +09:30
parent d3dac756d6
commit 04074ff372
2 changed files with 20 additions and 2 deletions

View File

@@ -0,0 +1,18 @@
$(document).ready(function () {
$('.post-like').show();
$('.post-like').on('ajax:success', function(event, data) {
var like_control = $('#post-' + data.id + ' .post-like');
$('#post-' + data.id + ' .like-count').text(data.description);
if (data.liked_by_member) {
like_control.data("method", "delete");
like_control.attr("href", data.url);
like_control.text("Unlike");
} else {
like_control.data("method", "post");
like_control.attr("href", '/likes.json?post_id=' + data.id);
like_control.text("Like");
}
});
});

View File

@@ -39,8 +39,8 @@
= pluralize(post.likes.count, "like") unless post.likes.empty?
- if member_signed_in?
- if !post.members.include? current_member
= link_to 'Like', likes_path(Like.new, post_id: post.id, format: :json), method: :post, remote: true, class: 'post-like', style: "display: none"
= link_to 'Like', likes_path(Like.new, post_id: post.id, format: :json), method: :post, remote: true, class: 'post-like'#, style: "display: none"
- else
- like = post.likes.find_by(member: current_member)
- if like
= link_to 'Unlike', like_path(like, format: :json), method: :delete, remote: true, class: 'post-like', style: "display: none"
= link_to 'Unlike', like_path(like, format: :json), method: :delete, remote: true, class: 'post-like'#, style: "display: none"