gpt4 book ai didi

ruby-on-rails - 自定义错误重定向,闪光通知不起作用

转载 作者:行者123 更新时间:2023-12-04 18:00:12 26 4
gpt4 key购买 nike

在一个经典的 Rails 应用程序中,我尝试使用一个自定义 Controller 来处理错误,该 Controller 通过一条闪现消息重定向到 root_path。

路线:

match "/404", :to => "errors#not_found", :via => :all
match "/500", :to => "errors#internal_server_error", :via => :all

错误 Controller :

class ErrorsController < Admin::AdminController
def not_found
redirect_to admin_path, notice: "Sorry, reccord not found"
end

def internal_server_error
redirect_to root_path, notice: "Sorry, something went wrong"
end
end

redirect_to admin_path or root_path 的错误(我试过其他路径确定不相关)

管理路径只显示一个仪表板:

class Admin::StaticController < Admin::AdminController
def dashboard
flash.keep
byebug
end
end

即使没有多重重定向,我也尝试添加 flash.keep。 Byebug 停止进程以帮助查看发生了什么,但此时闪现通知显示为零。

日志(稍作清理):

Started GET something_not_possible_to_get
Completed 404 Not Found in 13ms (ActiveRecord: 2.3ms)
ActiveRecord::RecordNotFound
Processing by ErrorsController#not_found as HTML
Redirected to the_path
Started GET "/" (root_path)
ByeBug stopping here and notice is nil
continue just render the page as expected but without the notice

我想要实现的是将出现错误(404 或 500)的用户重定向到 root_path 或其他路径,让他们知道通知消息(flash)出了问题,但不会阻止他们出现经典的 Rails 错误页。然后我就可以实现内部错误处理(比如给应用程序的管理员/所有者发送电子邮件)。

我尝试了 keep.flash 但没有成功。

为什么通知消息被丢弃?以及为什么只有在出现错误时才会发生这种情况(我有很多带有通知的 redirect_to 工作正常)?

最佳答案

您不必创建 ErrorsController,相反您可以捕获错误并执行您想要的任何操作。

您还可以使用 exception_notification gem 将错误相关的邮件发送到配置的帐户。

在应用程序 Controller 中

class ApplicationController < ActionController::Base 
rescue_from ActiveRecord::RecordNotFound, :with => :not_found
rescue_from ActiveRecord::RecordInvalid, :with => :internal_server_error
#some other errors that can be captured
#rescue_from ActionController::MissingFile, :with => :render_404
#rescue_from ActionController::RoutingError, :with => :render_404
#rescue_from ActionView::TemplateError, :with => :render_500
#rescue_from Errno::ENOENT, :with => :render_404
#rescue_from Errno::EACCES, :with => :render_404

def not_found
notify(error)
redirect_to admin_path, notice: "Sorry, reccord not found"
end

def internal_server_error
notify(error)
redirect_to root_path, notice: "Sorry, something went wrong"
end

def notify(exception)
ExceptionNotifier::Notifier.exception_notification(request.env, exception,
data: {message: 'An error occured'}).deliver
end

end

设置 exception_notification gem

  1. 在 Gemfile 中添加 gem

  2. 设置邮件:

production.rb(在 development.rb 中实现相同的内容以进行测试):

MyApp::Application.configure do
#Action Mailer configuration to send emails
config.action_mailer.delivery_method = :smtp

config.action_mailer.smtp_settings = {
:address => "smtp.gmail.com",
:port => 587,
:domain => "google.com",
:user_name => "xyz@gmail.com",
:password => "123",
:authentication => "plain",
:enable_starttls_auto => true
}
end

MyApp::Application.config.middleware.use ExceptionNotification::Rack,
:email => {
:email_prefix => "[error in]",
:sender_address => %{xyz@gmail.com},
:exception_recipients => %w{xyz@gmail.com}
}
  1. 在答案开头检查应用程序 Controller 中的上述代码。

关于ruby-on-rails - 自定义错误重定向,闪光通知不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36429153/

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