You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
educoder/app/controllers/praise_tread_controller.rb

47 lines
1.4 KiB

6 years ago
class PraiseTreadController < ApplicationController
include MessagesHelper
6 years ago
before_action :require_login, :check_auth
6 years ago
before_action :validate_params, only: [:like, :unlike]
before_action :find_object
def like
begin
return normal_status(2, "你已点过赞了") if by_user_liked?(@obj, current_user)
@praise_tread = @obj.praise_treads.build(user_id: current_user.id)
@praise_tread.save!
rescue Exception => e
uid_logger_error(e.message)
tip_exception(e.message)
raise ActiveRecord::Rollback
end
end
def unlike
begin
return normal_status(2, "你还没有点过赞噢") unless by_user_liked?(@obj, current_user)
@praise_treads = @obj.praise_treads.user_liker(current_user)
@praise_treads.last.destroy! if @praise_treads.present?
rescue Exception => e
uid_logger_error(e.message)
tip_exception(e.message)
raise ActiveRecord::Rollback
end
end
private
def find_object
begin
@obj = params[:object_type].strip.classify.constantize.find params[:object_id]
rescue Exception => e
uid_logger_error(e.message)
tip_exception(e.message)
return
end
end
def validate_params
return normal_status(2, "缺少参数object_id") if params[:object_id].blank?
return normal_status(2, "缺少参数object_type") if params[:object_type].blank?
end
end