Use rescue_from in CommentsController

This commit is contained in:
Raphael Sofaer 2011-06-14 18:06:13 -07:00
parent 800e701f89
commit cba63030c9
2 changed files with 22 additions and 15 deletions

View file

@ -9,6 +9,10 @@ class CommentsController < ApplicationController
respond_to :html, :mobile respond_to :html, :mobile
respond_to :json, :only => :show respond_to :json, :only => :show
rescue_from ActiveRecord::RecordNotFound do
render :nothing => true, :status => 404
end
def create def create
target = current_user.find_visible_post_by_id params[:post_id] target = current_user.find_visible_post_by_id params[:post_id]
text = params[:text] text = params[:text]
@ -17,7 +21,8 @@ class CommentsController < ApplicationController
@comment = current_user.build_comment(:text => text, :post => target) @comment = current_user.build_comment(:text => text, :post => target)
if @comment.save 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 Postzord::Dispatch.new(current_user, @comment).post
respond_to do |format| respond_to do |format|
@ -34,21 +39,18 @@ class CommentsController < ApplicationController
end end
def destroy def destroy
if @comment = Comment.where(:id => params[:id]).first @comment = Comment.find(params[:id])
if current_user.owns?(@comment) || current_user.owns?(@comment.parent) if current_user.owns?(@comment) || current_user.owns?(@comment.parent)
current_user.retract(@comment) current_user.retract(@comment)
respond_to do |format| respond_to do |format|
format.mobile{ redirect_to @comment.post } format.mobile{ redirect_to @comment.post }
format.js {render :nothing => true, :status => 204} 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
end end
else 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
end end

View file

@ -111,5 +111,10 @@ describe CommentsController do
response.status.should == 403 response.status.should == 403
end end
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
end end