commit
18306783ab
|
@ -1,29 +1,63 @@
|
||||||
|
#encoding: utf-8
|
||||||
class CommentsController < ApplicationController
|
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
|
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
|
end
|
||||||
|
|
||||||
def edit
|
def edit
|
||||||
|
respond_to do |format|
|
||||||
|
format.js
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def update
|
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
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
|
@result = {status: false, message: ''}
|
||||||
|
if @comment.destroy
|
||||||
|
@result[:status] = true
|
||||||
|
else
|
||||||
|
@result[:message] = '删除评论失败'
|
||||||
|
end
|
||||||
|
respond_to do |format|
|
||||||
|
format.js
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def article
|
def article
|
||||||
|
@article = Article.find params[:article_id]
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_comment
|
def get_comment
|
||||||
|
@comment = @article.comments.find params[:id]
|
||||||
end
|
end
|
||||||
end
|
end
|
|
@ -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})}");
|
|
@ -0,0 +1,3 @@
|
||||||
|
- if @result[:status]
|
||||||
|
:plain
|
||||||
|
$("#comment_#{@comment.id}").remove();
|
|
@ -0,0 +1,3 @@
|
||||||
|
:plain
|
||||||
|
$("#comment_form").html("#{escape_javascript(render partial: 'articles/comment_form')}");
|
||||||
|
location.hash = "#comment_form";
|
|
@ -0,0 +1,4 @@
|
||||||
|
- if @result[:status]
|
||||||
|
:plain
|
||||||
|
$("#comment_#{@comment.id}_body").html("#{@comment.content}");
|
||||||
|
location.hash = "#comment_#{@comment.id}";
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue