class CommentsController < ApplicationController before_action :set_comment, only: [:show, :edit, :update, :destroy] # GET /comments def index @comments = Comment.where(is_approved: true).all end # GET /comments/1 def show end # GET /comments/new def new @comment = Comment.new end # GET /comments/1/edit def edit end # POST /comments def create create_params = comment_params.to_hash.deep_symbolize_keys create_params[:content] = CensorBear::StopWord.filter(create_params[:content], 'ugc') @comment = Comment.new(create_params) @comment.user = current_user if @comment.save text_data_id = CensorBear::TextDataId.encode(@comment, user_id: @comment.user.id, ip_address: request.ip) r = CensorBear::CheckText.new(@comment.content, text_data_id, "ugc").scan if r.review @comment.update_columns(is_approved: false) end redirect_to @comment, notice: "Comment was successfully created." else render :new end end # PATCH/PUT /comments/1 def update update_params = comment_params.to_hash.deep_symbolize_keys if @comment.update(update_params) redirect_to @comment, notice: "Comment was successfully updated." else render :edit end end # DELETE /comments/1 def destroy @comment.destroy redirect_to comments_url, notice: "Comment was successfully destroyed." end private # Use callbacks to share common setup or constraints between actions. def set_comment @comment = Comment.find(params[:id]) end # Only allow a list of trusted parameters through. def comment_params params.require(:comment).permit(:content, :user_id, :is_approved) end end