点赞和隐藏

chromesetting
daiao 5 years ago
parent 442eb24fc0
commit 42357d1f74

@ -43,6 +43,16 @@ class CommentsController < ApplicationController
render_ok render_ok
end end
# 隐藏、取消隐藏
def hidden
if @hack.manager?(current_user)
@discuss.update_attribute(:hidden, params[:hidden] == "1")
sucess_status
else
Educoder::TipException(403, "..")
end
end
private private
def find_hack def find_hack

@ -117,13 +117,13 @@ class DiscussesController < ApplicationController
# 0 取消赞; # 0 取消赞;
def plus def plus
pt = PraiseTread.where(:praise_tread_object_id => params[:id], :praise_tread_object_type => params[:container_type], pt = PraiseTread.where(:praise_tread_object_id => params[:id], :praise_tread_object_type => params[:container_type],
:user_id => current_user, :praise_or_tread => 1).first :user_id => current_user, :praise_or_tread => 1)
# 如果当前用户已赞过,则不能重复赞 # 如果当前用户已赞过,则不能重复赞
if params[:type] == 1 && pt.blank? if params[:type] == 1 && pt.blank?
PraiseTread.create!(:praise_tread_object_id => params[:id], :praise_tread_object_type => params[:container_type], PraiseTread.create!(:praise_tread_object_id => params[:id], :praise_tread_object_type => params[:container_type],
:user_id => current_user.id, :praise_or_tread => 1) if pt.blank? :user_id => current_user.id, :praise_or_tread => 1)
else else
pt.destroy if pt.present? # 如果已赞过,则删掉这条赞(取消);如果没赞过,则为非法请求不处理 pt.destroy_all if pt.present? # 如果已赞过,则删掉这条赞(取消);如果没赞过,则为非法请求不处理
end end
@praise_count = PraiseTread.where(:praise_tread_object_id => params[:id], :praise_tread_object_type => params[:container_type], @praise_count = PraiseTread.where(:praise_tread_object_id => params[:id], :praise_tread_object_type => params[:container_type],

@ -11,6 +11,9 @@ class Hack < ApplicationRecord
has_many :hack_codes, :dependent => :destroy has_many :hack_codes, :dependent => :destroy
has_many :hack_user_lastest_codes, :dependent => :destroy has_many :hack_user_lastest_codes, :dependent => :destroy
has_many :discusses, as: :dis, dependent: :destroy has_many :discusses, as: :dis, dependent: :destroy
# 点赞
has_many :praise_treads, as: :praise_tread_object, dependent: :destroy
belongs_to :user belongs_to :user
scope :published, -> { where(status: 1) } scope :published, -> { where(status: 1) }

@ -0,0 +1 @@
json.praise_count @praise_count

@ -71,6 +71,7 @@ Rails.application.routes.draw do
resources :comments do resources :comments do
collection do collection do
post :reply post :reply
post :hidden
end end
end end
end end

@ -0,0 +1,5 @@
class AddPraisesCountForHacks < ActiveRecord::Migration[5.2]
def change
add_column :hacks, :praises_count, :integer, :default => 0
end
end
Loading…
Cancel
Save