gpt4 book ai didi

ruby-on-rails - 事件管理员 : undefined method `access_denied'

转载 作者:行者123 更新时间:2023-12-04 03:35:29 25 4
gpt4 key购买 nike

我使用 rails 5.0
康康 1.6.10
设计 4.2.0
事件管理员

我经常在 newrelic 中遇到此错误:

NoMethodError: undefined method `access_denied' for #<Admin::FollowupsController:0x007f112917d270>

在 active_admin.rb 中,我在配置中设置了 :access_denied :
  config.on_unauthorized_access = :access_denied

如何消除此错误并很好地管理 access_denied 而不是 500 的重定向?

最佳答案

正如您所配置的 ActiveAdmin使用 :access_denied未授权访问的方法,您需要在 application_controller.rb 中定义此方法并将用户从他们无权访问的页面重定向到他们有权访问的资源。您也可以在浏览器中显示错误消息。一个典型的例子:

class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

def access_denied(exception)
redirect_to admin_root_path, alert: exception.message
end
end

HTML请求重定向到首页,JSON请求返回403 Forbidden的例子:
def access_denied(exception)
respond_to do |format|
format.json { head :forbidden, content_type: 'text/html' }
format.html { redirect_to main_app.root_url, notice: exception.message }
end
end

如果您希望返回 403 Forbidden HTTP 代码,请创建 public/403.html文件并像这样渲染它:
def access_denied(exception)
render file: Rails.root.join('public', '403.html'),
status: 403,
layout: false
end

关于ruby-on-rails - 事件管理员 : undefined method `access_denied' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47202760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com