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 01/14] 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 From 5c92ef327e2ca3a05f8bb7e377bf1a8187823466 Mon Sep 17 00:00:00 2001 From: 2015E8018661112 <794811315@qq.com> Date: Sun, 12 Apr 2015 10:31:13 +0800 Subject: [PATCH 02/14] update --- app/views/comments/create.js.haml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100755 app/views/comments/create.js.haml diff --git a/app/views/comments/create.js.haml b/app/views/comments/create.js.haml new file mode 100755 index 0000000..7c7cde0 --- /dev/null +++ b/app/views/comments/create.js.haml @@ -0,0 +1,4 @@ +- if @result[:status] + $("#article_comment_content").val(''); + $('#no_comments').remove(); + $("#paginate").prepend("#{escape_javascript(render partial: 'articles/comment', locals: {comment: @comment})}"); \ No newline at end of file From f9e4ac15cfbf7f8fd82f86d12e9787e1d2221dbe Mon Sep 17 00:00:00 2001 From: 2015E8018661112 <794811315@qq.com> Date: Mon, 12 Oct 2015 16:05:36 +0800 Subject: [PATCH 03/14] update --- app/views/comments/destroy.js.haml | 3 +++ 1 file changed, 3 insertions(+) create mode 100755 app/views/comments/destroy.js.haml diff --git a/app/views/comments/destroy.js.haml b/app/views/comments/destroy.js.haml new file mode 100755 index 0000000..15d728b --- /dev/null +++ b/app/views/comments/destroy.js.haml @@ -0,0 +1,3 @@ +- if @result[:status] + :plain + $("#comment_#{@comment.id}").remove(); \ No newline at end of file From 0603e5e63e15a881f4cef4242d06b33fd9ab3f8c Mon Sep 17 00:00:00 2001 From: 2015E8018661112 <794811315@qq.com> Date: Mon, 21 Dec 2015 11:17:26 +0800 Subject: [PATCH 04/14] update --- app/views/comments/edit.js.haml | 3 +++ 1 file changed, 3 insertions(+) create mode 100755 app/views/comments/edit.js.haml diff --git a/app/views/comments/edit.js.haml b/app/views/comments/edit.js.haml new file mode 100755 index 0000000..9b4577f --- /dev/null +++ b/app/views/comments/edit.js.haml @@ -0,0 +1,3 @@ +:plain + $("#comment_form").html("#{escape_javascript(render partial: 'articles/comment_form')}"); + location.hash = "#comment_form"; \ No newline at end of file From 4ffc1b2dc2d627046172150065b9946cfe834b93 Mon Sep 17 00:00:00 2001 From: 2015E8018661112 <794811315@qq.com> Date: Thu, 10 Dec 2015 10:31:13 +0800 Subject: [PATCH 05/14] update --- app/views/comments/destroy.js.haml | 3 --- app/views/comments/edit.js.haml | 3 --- 2 files changed, 6 deletions(-) delete mode 100755 app/views/comments/destroy.js.haml delete mode 100755 app/views/comments/edit.js.haml diff --git a/app/views/comments/destroy.js.haml b/app/views/comments/destroy.js.haml deleted file mode 100755 index 15d728b..0000000 --- a/app/views/comments/destroy.js.haml +++ /dev/null @@ -1,3 +0,0 @@ -- if @result[:status] - :plain - $("#comment_#{@comment.id}").remove(); \ No newline at end of file diff --git a/app/views/comments/edit.js.haml b/app/views/comments/edit.js.haml deleted file mode 100755 index 9b4577f..0000000 --- a/app/views/comments/edit.js.haml +++ /dev/null @@ -1,3 +0,0 @@ -:plain - $("#comment_form").html("#{escape_javascript(render partial: 'articles/comment_form')}"); - location.hash = "#comment_form"; \ No newline at end of file From adcd1c955e95fe24ba652d25eafc1a3828f67086 Mon Sep 17 00:00:00 2001 From: 2015E8018661112 <794811315@qq.com> Date: Thu, 10 Dec 2015 10:31:13 +0800 Subject: [PATCH 06/14] update --- app/views/destroy.js.haml | 3 +++ 1 file changed, 3 insertions(+) create mode 100755 app/views/destroy.js.haml diff --git a/app/views/destroy.js.haml b/app/views/destroy.js.haml new file mode 100755 index 0000000..15d728b --- /dev/null +++ b/app/views/destroy.js.haml @@ -0,0 +1,3 @@ +- if @result[:status] + :plain + $("#comment_#{@comment.id}").remove(); \ No newline at end of file From 23b04a10e22e596e921a739001eece402258b528 Mon Sep 17 00:00:00 2001 From: 2015E8018661112 <794811315@qq.com> Date: Wed, 23 Dec 2015 18:06:54 +0800 Subject: [PATCH 07/14] update --- app/views/edit.js.haml | 3 +++ 1 file changed, 3 insertions(+) create mode 100755 app/views/edit.js.haml diff --git a/app/views/edit.js.haml b/app/views/edit.js.haml new file mode 100755 index 0000000..9b4577f --- /dev/null +++ b/app/views/edit.js.haml @@ -0,0 +1,3 @@ +:plain + $("#comment_form").html("#{escape_javascript(render partial: 'articles/comment_form')}"); + location.hash = "#comment_form"; \ No newline at end of file From a978ac5bfa0da2903ef2b79d2fa2c8ddbbdc9c62 Mon Sep 17 00:00:00 2001 From: 2015E8018661112 <794811315@qq.com> Date: Tue, 29 Dec 2015 07:39:12 +0800 Subject: [PATCH 08/14] update --- app/views/update.js.haml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100755 app/views/update.js.haml diff --git a/app/views/update.js.haml b/app/views/update.js.haml new file mode 100755 index 0000000..be98f3e --- /dev/null +++ b/app/views/update.js.haml @@ -0,0 +1,4 @@ +- if @result[:status] + :plain + $("#comment_#{@comment.id}_body").html("#{@comment.content}"); + location.hash = "#comment_#{@comment.id}"; \ No newline at end of file From c0c10c3913611ceacb27936032d4e927adf6d958 Mon Sep 17 00:00:00 2001 From: 2015E8018661112 <794811315@qq.com> Date: Wed, 9 Dec 2015 08:01:11 +0800 Subject: [PATCH 09/14] delete --- app/views/comments/create.js.haml | 4 ---- app/views/destroy.js.haml | 3 --- app/views/edit.js.haml | 3 --- app/views/update.js.haml | 4 ---- 4 files changed, 14 deletions(-) delete mode 100755 app/views/comments/create.js.haml delete mode 100755 app/views/destroy.js.haml delete mode 100755 app/views/edit.js.haml delete mode 100755 app/views/update.js.haml diff --git a/app/views/comments/create.js.haml b/app/views/comments/create.js.haml deleted file mode 100755 index 7c7cde0..0000000 --- a/app/views/comments/create.js.haml +++ /dev/null @@ -1,4 +0,0 @@ -- if @result[:status] - $("#article_comment_content").val(''); - $('#no_comments').remove(); - $("#paginate").prepend("#{escape_javascript(render partial: 'articles/comment', locals: {comment: @comment})}"); \ No newline at end of file diff --git a/app/views/destroy.js.haml b/app/views/destroy.js.haml deleted file mode 100755 index 15d728b..0000000 --- a/app/views/destroy.js.haml +++ /dev/null @@ -1,3 +0,0 @@ -- if @result[:status] - :plain - $("#comment_#{@comment.id}").remove(); \ No newline at end of file diff --git a/app/views/edit.js.haml b/app/views/edit.js.haml deleted file mode 100755 index 9b4577f..0000000 --- a/app/views/edit.js.haml +++ /dev/null @@ -1,3 +0,0 @@ -:plain - $("#comment_form").html("#{escape_javascript(render partial: 'articles/comment_form')}"); - location.hash = "#comment_form"; \ No newline at end of file diff --git a/app/views/update.js.haml b/app/views/update.js.haml deleted file mode 100755 index be98f3e..0000000 --- a/app/views/update.js.haml +++ /dev/null @@ -1,4 +0,0 @@ -- if @result[:status] - :plain - $("#comment_#{@comment.id}_body").html("#{@comment.content}"); - location.hash = "#comment_#{@comment.id}"; \ No newline at end of file From c527b59ba8053eae0075e49d7ee015d61d03bfc7 Mon Sep 17 00:00:00 2001 From: 2015E8018661112 <794811315@qq.com> Date: Thu, 10 Dec 2015 10:31:14 +0800 Subject: [PATCH 10/14] update --- app/views/comments/create.js.haml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100755 app/views/comments/create.js.haml diff --git a/app/views/comments/create.js.haml b/app/views/comments/create.js.haml new file mode 100755 index 0000000..7c7cde0 --- /dev/null +++ b/app/views/comments/create.js.haml @@ -0,0 +1,4 @@ +- if @result[:status] + $("#article_comment_content").val(''); + $('#no_comments').remove(); + $("#paginate").prepend("#{escape_javascript(render partial: 'articles/comment', locals: {comment: @comment})}"); \ No newline at end of file From 7d450cfddbe450321bfb12edd5e41177452fb03c Mon Sep 17 00:00:00 2001 From: 2015E8018661112 <794811315@qq.com> Date: Wed, 16 Dec 2015 07:12:41 +0800 Subject: [PATCH 11/14] update --- app/views/comments/destroy.js.haml | 3 +++ 1 file changed, 3 insertions(+) create mode 100755 app/views/comments/destroy.js.haml diff --git a/app/views/comments/destroy.js.haml b/app/views/comments/destroy.js.haml new file mode 100755 index 0000000..15d728b --- /dev/null +++ b/app/views/comments/destroy.js.haml @@ -0,0 +1,3 @@ +- if @result[:status] + :plain + $("#comment_#{@comment.id}").remove(); \ No newline at end of file From f369ff59a54785ae6f96541461eab558cbbbf6cd Mon Sep 17 00:00:00 2001 From: 2015E8018661112 <794811315@qq.com> Date: Mon, 21 Dec 2015 16:22:17 +0800 Subject: [PATCH 12/14] update --- app/views/comments/edit.js.haml | 3 +++ 1 file changed, 3 insertions(+) create mode 100755 app/views/comments/edit.js.haml diff --git a/app/views/comments/edit.js.haml b/app/views/comments/edit.js.haml new file mode 100755 index 0000000..9b4577f --- /dev/null +++ b/app/views/comments/edit.js.haml @@ -0,0 +1,3 @@ +:plain + $("#comment_form").html("#{escape_javascript(render partial: 'articles/comment_form')}"); + location.hash = "#comment_form"; \ No newline at end of file From 6e444da0957ff229d63e428e5c32df3ae4204232 Mon Sep 17 00:00:00 2001 From: 2015E8018661112 <794811315@qq.com> Date: Mon, 28 Dec 2015 13:38:49 +0800 Subject: [PATCH 13/14] update --- app/views/comments/update.js.haml | 4 ++++ 1 file changed, 4 insertions(+) create mode 100755 app/views/comments/update.js.haml diff --git a/app/views/comments/update.js.haml b/app/views/comments/update.js.haml new file mode 100755 index 0000000..be98f3e --- /dev/null +++ b/app/views/comments/update.js.haml @@ -0,0 +1,4 @@ +- if @result[:status] + :plain + $("#comment_#{@comment.id}_body").html("#{@comment.content}"); + location.hash = "#comment_#{@comment.id}"; \ No newline at end of file From e24fcea540a61f9f0d77ed16bd02d2d24bf664b2 Mon Sep 17 00:00:00 2001 From: 2015E8018661112 <794811315@qq.com> Date: Sat, 2 Jan 2016 08:15:42 +0800 Subject: [PATCH 14/14] update test file --- test/controllers/comments_controller_test.rb | 46 ++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 test/controllers/comments_controller_test.rb diff --git a/test/controllers/comments_controller_test.rb b/test/controllers/comments_controller_test.rb new file mode 100644 index 0000000..a6dd4d3 --- /dev/null +++ b/test/controllers/comments_controller_test.rb @@ -0,0 +1,46 @@ +//comments_controller_test.rb +require 'test_helper' +require 'articles_controller' +require 'comments_controller' + +class CommentsControllerTest < ActionController::TestCase + setup do + @controller = CommentsController.new + @comment = Comment.new + @comment .content = 'comment1' + @comment.status = true + @comment.message = 'message1' + @comment.save + end + + test “should create comment” do + post:create,:comment=>{:content=>'content1',:status=>true,:message=>'message1'} + assert_not_nil session[:comment_id] + assert_redirected_to comment_path(@comment) + end + + test “should edit comment” do + get:edit,id:@comment.id + assert_response:success + end + + test “should update comment” do + get:update,id:@comment.id + assert_response:success + end + + test “should destroy comment” do + assert_difference('Comment.count',-1) do + delete:destroy,id:@comment.id + end + + assert_redirected_to comments_path + end + + test “destroy_wrong” do + post:destroy,:comment=>{:content=>'content1'} + assert_response:success + assert_equal '删除失败',flash[:error] + end +end +