增加 liked_count

This commit is contained in:
yafeilee 2014-03-31 12:25:22 +08:00
parent 0e523d6ff4
commit bbb3f213f9
4 changed files with 13 additions and 9 deletions

View File

@ -16,12 +16,12 @@
$http.post url
.success (res)->
if res.success
$scope.count += 1
$scope.like = $cookies.like = res.id
$scope.count = res.count
$scope.cancel = ->
$http.delete url + "/" + $scope.like
$http.delete url + "/" + $scope.like, (res)->
$scope.count = res.count
# anyway, clear cookie
delete $cookies["like"]
$scope.like = null
$scope.get_count()

View File

@ -51,7 +51,7 @@ class ArchivesController < ApplicationController
type: post.type,
created_at: format_date(post.created_at),
id: post.id.to_s,
liked_count: post.likes.size,
liked_count: post.liked_count,
visited_count: post.visited_count,
labels: post.labels_content
}

View File

@ -3,7 +3,7 @@ class LikesController < ApplicationController
def index
post = Post.find( params[:blog_id] )
render :json=> { success: true, count: post.likes.size }
render :json=> { success: true, count: post.liked_count }
end
def create
@ -11,9 +11,9 @@ class LikesController < ApplicationController
like = Like.new
like.post = post
if like.save
render :json=> { success: true, id: like.id.to_s }
render :json=> { success: true, id: like.id.to_s, count: post.liked_count }
else
render :json=> { success: false }
render :json=> { success: false, count: post.liked_count }
end
end
@ -21,9 +21,9 @@ class LikesController < ApplicationController
post = Post.find( params[:blog_id] )
like = post.likes.find(params[:id])
if like.destroy
render :json=> { success: true }
render :json=> { success: true, count: post.liked_count }
else
render :json=> { success: false }
render :json=> { success: false, count: post.liked_count }
end
end
end

View File

@ -41,4 +41,8 @@ class Post
def labels_content
self.labels.collect { |label| label.name }.join(", ")
end
def liked_count
self.likes.size
end
end