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 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 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 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 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 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 +