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 'carrierwave'
|
||||||
gem 'kaminari', git: 'git@github.com:amatsuda/kaminari.git'
|
gem 'kaminari', git: 'git@github.com:amatsuda/kaminari.git'
|
||||||
gem 'turbolinks', '~> 5.x'
|
gem 'turbolinks', '~> 5.x'
|
||||||
|
gem 'js_cookie_rails'
|
||||||
|
|
||||||
gem 'jbuilder'
|
gem 'jbuilder'
|
||||||
gem 'pg'
|
gem 'pg'
|
||||||
|
|
|
@ -47,7 +47,6 @@ GEM
|
||||||
minitest (~> 5.1)
|
minitest (~> 5.1)
|
||||||
tzinfo (~> 1.1)
|
tzinfo (~> 1.1)
|
||||||
addressable (2.4.0)
|
addressable (2.4.0)
|
||||||
angularjs-rails (1.5.0)
|
|
||||||
arel (7.0.0)
|
arel (7.0.0)
|
||||||
babel-source (5.8.35)
|
babel-source (5.8.35)
|
||||||
babel-transpiler (0.7.0)
|
babel-transpiler (0.7.0)
|
||||||
|
@ -135,6 +134,8 @@ GEM
|
||||||
rails-dom-testing (>= 1, < 3)
|
rails-dom-testing (>= 1, < 3)
|
||||||
railties (>= 4.2.0)
|
railties (>= 4.2.0)
|
||||||
thor (>= 0.14, < 2.0)
|
thor (>= 0.14, < 2.0)
|
||||||
|
js_cookie_rails (1.0.1)
|
||||||
|
railties (>= 3.1)
|
||||||
json (1.8.3)
|
json (1.8.3)
|
||||||
listen (3.0.6)
|
listen (3.0.6)
|
||||||
rb-fsevent (>= 0.9.3)
|
rb-fsevent (>= 0.9.3)
|
||||||
|
@ -318,7 +319,6 @@ PLATFORMS
|
||||||
ruby
|
ruby
|
||||||
|
|
||||||
DEPENDENCIES
|
DEPENDENCIES
|
||||||
angularjs-rails
|
|
||||||
byebug
|
byebug
|
||||||
capybara
|
capybara
|
||||||
carrierwave
|
carrierwave
|
||||||
|
@ -337,6 +337,7 @@ DEPENDENCIES
|
||||||
html_truncator
|
html_truncator
|
||||||
jbuilder
|
jbuilder
|
||||||
jquery-rails
|
jquery-rails
|
||||||
|
js_cookie_rails
|
||||||
kaminari!
|
kaminari!
|
||||||
listen (~> 3.0.5)
|
listen (~> 3.0.5)
|
||||||
mina
|
mina
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
//= require jquery_ujs
|
//= require jquery_ujs
|
||||||
//= require turbolinks
|
//= require turbolinks
|
||||||
//= require foundation
|
//= require foundation
|
||||||
|
//= require js.cookie
|
||||||
//= require 'jquery.html5-fileupload'
|
//= require 'jquery.html5-fileupload'
|
||||||
//= require_tree .
|
//= require_tree .
|
||||||
|
|
||||||
|
|
|
@ -8,13 +8,6 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
/*
|
|
||||||
本插件操作 textarea 或者 input 内的插入符
|
|
||||||
只实现了获得插入符在文本框中的位置,我设置
|
|
||||||
插入符的位置.
|
|
||||||
*/
|
|
||||||
|
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
(function(factory) {
|
(function(factory) {
|
||||||
if (typeof define === 'function' && define.amd) {
|
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
|
class ArchivesController < ApplicationController
|
||||||
def index
|
def index
|
||||||
@posts = Post.order(created_at: :desc).page(params[:page]).per(3)
|
@posts = Post.order(created_at: :desc).page(params[:page])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,15 +6,6 @@ class LikesController < ApplicationController
|
||||||
render :json=> { success: true, count: post.liked_count }
|
render :json=> { success: true, count: post.liked_count }
|
||||||
end
|
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
|
def create
|
||||||
post = Post.find( params[:blog_id] )
|
post = Post.find( params[:blog_id] )
|
||||||
like = post.likes.build
|
like = post.likes.build
|
||||||
|
|
|
@ -52,4 +52,8 @@ class Post < ActiveRecord::Base
|
||||||
def liked_count
|
def liked_count
|
||||||
self.likes.size
|
self.likes.size
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def liked_by?(like_id)
|
||||||
|
!! self.likes.where(id: like_id).first
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,8 +10,9 @@ p.ptag.published-at
|
||||||
= render 'common/copyright'
|
= render 'common/copyright'
|
||||||
hr.blog-over
|
hr.blog-over
|
||||||
p
|
p
|
||||||
button.button.like-button type='button'
|
button.button.like-button class="#{'liked' if post.liked_by?(cookies[:like])}" type='button' data-url=blog_likes_path(post)
|
||||||
| #{@likes_count} Like
|
span.count #{@likes_count}
|
||||||
|
span Like
|
||||||
.qrcode
|
.qrcode
|
||||||
a#qrcode-link href="#"
|
a#qrcode-link href="#"
|
||||||
i.fi-link
|
i.fi-link
|
||||||
|
|
Loading…
Reference in New Issue