我正在使用Bootstrap,它有一个
我还为评论添加了一个AJAX删除操作,并在其上添加了授权。当我尝试删除当前用户无权访问的评论时,操作不起作用 - 这是正确的。
但我希望发生错误时在Bootstrap样式的
div class="alert notice"
,用于各种提示消息的类。我还为评论添加了一个AJAX删除操作,并在其上添加了授权。当我尝试删除当前用户无权访问的评论时,操作不起作用 - 这是正确的。
但我希望发生错误时在Bootstrap样式的
中弹出一个错误消息,持续5至10秒,然后消失。
这是我的
这是我的
CommentsController.rb
上的destroy
操作。 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
在同一个控制器中的私有方法中设置了@comment
:
private
def set_comment
@comment = current_user.comments.find(params[:id])
end
这是我的 comments/destroy.js.erb
$('.delete_comment').bind('ajax:success', function() {
$(this).closest('div#new_comment').fadeOut();
});
但是这并不影响未经授权的访问。
在我的ability.rb
文件中,我有以下代码:
can :manage, Comment, user_id: user.id
当我尝试删除一条我无权访问的评论时,我的日志会显示以下内容:
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]:
这非常完美。
我想要做的就是在 Bootstrap 警告框中显示一个适当的错误信息,并在几秒钟后消失。
我该如何实现?
ActiveRecord :: RecordNotFound
错误,并且前端没有任何反应。顺便说一下,我正在使用pnotify-rails
gem - https://github.com/navinpeiris/pnotify-rails - marcamillionbefore_action :set_comment
之前还是之后执行load_and_authorize_resource
? - marcamillionbefore_action :set_comment
。load_and_authorize_resource
执行相同的任务。 - AnatoliiDability.rb
规则不足? - marcamillion