Like feature done

This commit is contained in:
yafeilee 2016-04-21 22:11:18 +08:00
parent 27314da99a
commit e1060b93af
9 changed files with 34 additions and 23 deletions

View File

@ -14,6 +14,7 @@ gem 'font-awesome-sass'
gem 'carrierwave'
gem 'kaminari', git: 'git@github.com:amatsuda/kaminari.git'
gem 'turbolinks', '~> 5.x'
gem 'js_cookie_rails'
gem 'jbuilder'
gem 'pg'

View File

@ -47,7 +47,6 @@ GEM
minitest (~> 5.1)
tzinfo (~> 1.1)
addressable (2.4.0)
angularjs-rails (1.5.0)
arel (7.0.0)
babel-source (5.8.35)
babel-transpiler (0.7.0)
@ -135,6 +134,8 @@ GEM
rails-dom-testing (>= 1, < 3)
railties (>= 4.2.0)
thor (>= 0.14, < 2.0)
js_cookie_rails (1.0.1)
railties (>= 3.1)
json (1.8.3)
listen (3.0.6)
rb-fsevent (>= 0.9.3)
@ -318,7 +319,6 @@ PLATFORMS
ruby
DEPENDENCIES
angularjs-rails
byebug
capybara
carrierwave
@ -337,6 +337,7 @@ DEPENDENCIES
html_truncator
jbuilder
jquery-rails
js_cookie_rails
kaminari!
listen (~> 3.0.5)
mina

View File

@ -2,6 +2,7 @@
//= require jquery_ujs
//= require turbolinks
//= require foundation
//= require js.cookie
//= require 'jquery.html5-fileupload'
//= require_tree .

7
app/assets/javascripts/jquery.atwho.js Executable file → Normal file
View File

@ -8,13 +8,6 @@
*/
/*
本插件操作 textarea 或者 input 内的插入符
只实现了获得插入符在文本框中的位置我设置
插入符的位置.
*/
(function() {
(function(factory) {
if (typeof define === 'function' && define.amd) {

View File

@ -0,0 +1,19 @@
$(document).on 'turbolinks:load', ->
$('.like-button').click ->
if $(this).hasClass('liked')
$.ajax
url: $(this).data('url') + '/' + Cookies.get('like')
type: 'DELETE'
success: (res)=>
$(this).removeClass('liked')
$(this).children('.count').text(res.count)
Cookies.remove('like')
else
$.ajax
url: $(this).data('url')
type: 'POST'
success: (res)=>
$(this).addClass('liked')
$(this).children('.count').text(res.count)
Cookies.set('like', res.id)

View File

@ -1,5 +1,5 @@
class ArchivesController < ApplicationController
def index
@posts = Post.order(created_at: :desc).page(params[:page]).per(3)
@posts = Post.order(created_at: :desc).page(params[:page])
end
end

View File

@ -6,15 +6,6 @@ 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 = post.likes.build

View File

@ -52,4 +52,8 @@ class Post < ActiveRecord::Base
def liked_count
self.likes.size
end
def liked_by?(like_id)
!! self.likes.where(id: like_id).first
end
end

View File

@ -10,8 +10,9 @@ p.ptag.published-at
= render 'common/copyright'
hr.blog-over
p
button.button.like-button type='button'
| #{@likes_count} Like
button.button.like-button class="#{'liked' if post.liked_by?(cookies[:like])}" type='button' data-url=blog_likes_path(post)
span.count #{@likes_count}
span Like
.qrcode
a#qrcode-link href="#"
i.fi-link