diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 9ddd5751c..8953d40b9 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -9,6 +9,10 @@ class CommentsController < ApplicationController respond_to :html, :mobile respond_to :json, :only => :show + rescue_from ActiveRecord::RecordNotFound do + render :nothing => true, :status => 404 + end + def create target = current_user.find_visible_post_by_id params[:post_id] text = params[:text] @@ -17,7 +21,8 @@ class CommentsController < ApplicationController @comment = current_user.build_comment(:text => text, :post => target) if @comment.save - Rails.logger.info("event=create type=comment user=#{current_user.diaspora_handle} status=success comment=#{@comment.id} chars=#{params[:text].length}") + Rails.logger.info(:event => :create, :type => :comment, :user => current_user.diaspora_handle, + :status => :success, :comment => @comment.id, :chars => params[:text].length) Postzord::Dispatch.new(current_user, @comment).post respond_to do |format| @@ -34,21 +39,18 @@ class CommentsController < ApplicationController end def destroy - if @comment = Comment.where(:id => params[:id]).first - if current_user.owns?(@comment) || current_user.owns?(@comment.parent) - current_user.retract(@comment) - respond_to do |format| - format.mobile{ redirect_to @comment.post } - format.js {render :nothing => true, :status => 204} - end - else - respond_to do |format| - format.mobile {redirect_to :back} - format.js {render :nothing => true, :status => 403} - end + @comment = Comment.find(params[:id]) + if current_user.owns?(@comment) || current_user.owns?(@comment.parent) + current_user.retract(@comment) + respond_to do |format| + format.mobile{ redirect_to @comment.post } + format.js {render :nothing => true, :status => 204} end else - render :nothing => true, :status => 404 + respond_to do |format| + format.mobile {redirect_to :back} + format.js {render :nothing => true, :status => 403} + end end end diff --git a/spec/controllers/comments_controller_spec.rb b/spec/controllers/comments_controller_spec.rb index a857e886d..8b1f3dd18 100644 --- a/spec/controllers/comments_controller_spec.rb +++ b/spec/controllers/comments_controller_spec.rb @@ -111,5 +111,10 @@ describe CommentsController do response.status.should == 403 end end + it 'renders nothing and 404 on a nonexistent comment' do + delete :destroy, :id => 343415 + response.status.should == 404 + response.body.strip.should be_empty + end end -end \ No newline at end of file +end