class PraiseTreadController < ApplicationController include MessagesHelper before_action :require_login, :check_auth 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