我在我的rails应用程序中使用CanCanCan进行授权。路由和重定向工作正常,但我无法显示AccessDenied错误消息(以前曾起作用,但我在途中一定搞砸了,现在它不会显示)。
这是我的代码:
Controller /application_controller.rb
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
rescue_from CanCan::AccessDenied do |exception|
redirect_to root_url, error: exception.message
end
...
end
(我将默认的
:alert
更改为:error
,否则我将收到来自devise的警报(“您已经登录”)。views/index.html.haml (根)
= flash[:error] if flash[:error]
...
关于如何使其重新工作的任何想法?提前致谢。
---编辑---
我尝试过的一些其他修复程序(均未成功):
error
和message
替换notice
rescue_from CanCan::AccessDenied
方法-它使我进入标准的CanCan错误页面(所以我知道该方法本身正在工作)。 exception.message
。 - if flash[:error].blank?
%p flash hash is blank
哈希确实是空的,因此似乎根本没有保存闪存。然后我加了
flash.keep(:error)
在我的 Controller 中,但没有任何改变。
最佳答案
您是否通过专用浏览器尝试过以防Cookie困惑?
可以尝试这种格式吗?
redirect_to(root_url, {:flash => { :error => exception.message }})
关于ruby-on-rails - Rails : CanCanCan - AccessDenied error not displaying,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50619472/