Like feature done
This commit is contained in:
parent
27314da99a
commit
e1060b93af
1
Gemfile
1
Gemfile
|
@ -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'
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
//= require jquery_ujs
|
||||
//= require turbolinks
|
||||
//= require foundation
|
||||
//= require js.cookie
|
||||
//= require 'jquery.html5-fileupload'
|
||||
//= require_tree .
|
||||
|
||||
|
|
|
@ -8,13 +8,6 @@
|
|||
*/
|
||||
|
||||
|
||||
/*
|
||||
本插件操作 textarea 或者 input 内的插入符
|
||||
只实现了获得插入符在文本框中的位置,我设置
|
||||
插入符的位置.
|
||||
*/
|
||||
|
||||
|
||||
(function() {
|
||||
(function(factory) {
|
||||
if (typeof define === 'function' && define.amd) {
|
||||
|
|
|
@ -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)
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue