修正like不同的博客时互相影响的问题

This commit is contained in:
yafeilee 2014-03-31 23:11:40 +08:00
parent e6fcc62d39
commit 0252887a0f
4 changed files with 33 additions and 11 deletions

View File

@ -1,28 +1,37 @@
@app.controller 'LikesController', ($scope, $http, $location, $cookies)->
url = $location.absUrl() + "/likes"
$scope.count = 0
$scope.get_count = ->
$http.get url
.success (res)->
$scope.count = res.count
$scope.get_count()
$http.get url
.success (res)->
$scope.count = res.count
$scope.like = $cookies.like
if $scope.like
$http
url: $location.absUrl() + "/likes/#{$scope.like}/is_liked"
method: 'GET'
.success (res)->
if res == 'true'
$scope.is_liked = true
else
$scope.is_liked = false
else
$scope.is_liked = false
$scope.submit = ->
$http.post url
.success (res)->
if res.success
$scope.like = $cookies.like = res.id
$scope.count = res.count
$scope.is_liked = true
$scope.cancel = ->
$http.delete url + "/" + $scope.like
.success (res)->
$scope.count = res.count
$scope.is_liked = false
# anyway, clear cookie
delete $cookies["like"]
$scope.like = null

View File

@ -6,6 +6,15 @@ class LikesController < ApplicationController
render :json=> { success: true, count: post.liked_count }
end
def is_liked
post = Post.find( params[:blog_id] )
if post.likes.where(id: params[:id]).first
render text: true
else
render text: false
end
end
def create
post = Post.find( params[:blog_id] )
like = Like.new

View File

@ -13,9 +13,9 @@ p.ptag
| 发表于:
span #{format_date(post.created_at)}
p ng-controller="LikesController"
button.like-button ng-show="! like " ng-click="submit()"
button.like-button ng-show="! is_liked " ng-click="submit()"
|{{ count }}
span Like
button.like-button ng-show=" like " ng-click="cancel()"
button.like-button ng-show=" is_liked " ng-click="cancel()"
|{{ count }}
span Liked

View File

@ -6,7 +6,11 @@ WBlog::Application.routes.draw do
get :rss
end
resources :comments, only: [:index, :create]
resources :likes, only: [:index, :create, :destroy]
resources :likes, only: [:index, :create, :destroy] do
member do
get :is_liked
end
end
end