From e8f452bb749afefeeeb0dce35534f8f9f1d619c6 Mon Sep 17 00:00:00 2001 From: 2015E8018661112 <794811315@qq.com> Date: Fri, 1 Jan 2016 20:23:22 -0600 Subject: [PATCH] update --- app/controllers/comments_controller.rb | 48 ++++++++++++++++++++++---- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 41ed086..b70968f 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -1,29 +1,63 @@ +#encoding: utf-8 class CommentsController < ApplicationController - + before_filter :article, only: [:create, :edit, :update, :destroy] + before_filter :require_login + before_filter :get_comment, only: [:edit, :update, :destroy] def create - + @result = {status: true, message: ''} + @comment = @article.comments.new content: params[:article_comment][:content] + @comment.user_id = @current_user.id + if @comment.save + @result[:status] = true + else + @result[:message] = '发表评论失败' + end + respond_to do |format| + format.js + end end def edit - + respond_to do |format| + format.js + end end def update - + @result = {status: false, message: ''} + if params[:article_comment].present? && params[:article_comment][:content].present? + @comment.content = params[:article_comment][:content] + if @comment.save + @result[:status] = true + else + @result[:message] = '编辑评论失败' + end + end + respond_to do |format| + format.js + end end def destroy - + @result = {status: false, message: ''} + if @comment.destroy + @result[:status] = true + else + @result[:message] = '删除评论失败' + end + respond_to do |format| + format.js + end end protected def article - + @article = Article.find params[:article_id] end def get_comment - + @comment = @article.comments.find params[:id] end end \ No newline at end of file