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/comments_controller.rb

61 lines
1.4 KiB

5 years ago
class CommentsController < ApplicationController
before_action :find_hack
before_action :require_login
# 评论
def create
begin
@discuss = @hack.discusses.new(comment_params) # 管理员回复的能够显示
@discuss.hidden = false
@discuss.user_id = current_user.id
@discuss.save!
rescue Exception => e
uid_logger_error("create discuss failed : #{e.message}")
render_error("评论异常")
end
end
# 回复
def reply
begin
@discuss = @hack.discusses.new(reply_params)
@discuss.hidden = false
@discuss.user_id = current_user.id
@discuss.root_id = params[:parent_id]
@discuss.save!
rescue Exception => e
uid_logger_error("reply discuss failed : #{e.message}")
render_error("回复评论异常")
end
end
# 列表
def index
5 years ago
logger.info("##########hack:#{@hack.id}")
5 years ago
discusses = Discuss.where(root_id: nil, dis_id: @hack.id, dis_type: @hack.class.to_s)
5 years ago
@discusses_count = discusses.count
@discusses= paginate discusses
5 years ago
end
5 years ago
# 删除
def destroy
@hack.discusses.find_by(id: params[:id]).destroy
render_ok
end
5 years ago
private
def find_hack
5 years ago
@hack = Hack.find_by_identifier(params[:hack_identifier])
5 years ago
end
def comment_params
params.require(:comments).permit(:content)
end
def reply_params
params.require(:comments).permit(:content, :parent_id)
end
end