I am using Bootstrap, which has div class="alert notice"
that has a bunch of classes for various notice messages.
I also have an AJAX destroy action for a comment, that I have added cancan
authorization on. When I try to delete a comment that the current_user
doesn't have access to it doesn't work - which is correct.
But what I want to happen is for an error message to pop-up, in a Bootstrap style'd div for 5 - 10 seconds and then disappear.
This is the destroy
action on my CommentsController.rb
def destroy
respond_to do |format|
if @comment.destroy
format.html { redirect_to root_url, notice: 'Comment was successfully deleted.' }
format.json { head :no_content }
format.js { render :layout => false }
else
format.json { render json: @comment.errors, status: :unprocessable_entity }
end
end
end
Where I have the @comment
set in a private method in the same controller:
private
def set_comment
@comment = current_user.comments.find(params[:id])
end
This is my comments/destroy.js.erb
$('.delete_comment').bind('ajax:success', function() {
$(this).closest('div#new_comment').fadeOut();
});
But that doesn't affect unauthorized access.
In my ability.rb
, I have this:
can :manage, Comment, user_id: user.id
In my log when I try to delete a comment that I don't have access to, I get this in my log:
Started DELETE "/comments/5" for 127.0.0.1 at 2014-10-16 02:56:53 -0500
Processing by CommentsController#destroy as JS
Parameters: {"id"=>"5"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = 1 ORDER BY "users"."id" ASC LIMIT 1
FamilyTree Load (0.2ms) SELECT "family_trees".* FROM "family_trees" WHERE "family_trees"."user_id" = $1 LIMIT 1 [["user_id", 1]]
ReadMark Load (0.1ms) SELECT "read_marks".* FROM "read_marks" WHERE "read_marks"."user_id" = $1 AND "read_marks"."readable_type" = 'PublicActivity::ORM::ActiveRecord::Activity' AND "read_marks"."readable_id" IS NULL ORDER BY "read_marks"."id" ASC LIMIT 1 [["user_id", 1]]
Comment Load (0.3ms) SELECT "comments".* FROM "comments" WHERE "comments"."user_id" = $1 AND "comments"."id" = $2 LIMIT 1 [["user_id", 1], ["id", 5]]
Completed 404 Not Found in 8ms
ActiveRecord::RecordNotFound - Couldn't find Comment with 'id'=5 [WHERE "comments"."user_id" = $1]:
Which is perfect.
All I want to do is show an appropriate error in a Bootstrap alert that disappears in a few seconds.
How do I accomplish that?
ActiveRecord::RecordNotFound
error in my logs and nothing happens on the front-end. By the way, I am using thepnotify-rails
gem - github.com/navinpeiris/pnotify-rails – Shambles