在经典的Rails应用程序中,我尝试使用自定义控制器处理错误,并使用flash消息重定向到root_path。
路线:
match "/404", :to => "errors#not_found", :via => :all
match "/500", :to => "errors#internal_server_error", :via => :all
错误控制器:
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或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通知工作正常)?
答案 0 :(得分:1)
您不必创建ErrorsController,而是可以捕获错误并执行您想要的任何操作。
此外,您可以使用exception_notification
gem将错误相关邮件发送到配置的帐户。
在应用程序控制器中
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 ,
在Gemfile中添加gem
设置邮件:
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 :(得分:0)
为了使用这些路线,
match "/404", :to => "errors#not_found", :via => :all
match "/500", :to => "errors#internal_server_error", :via => :all
您必须在application.rb中设置异常处理程序:
module RailsApp
class Application < Rails::Application
config.exceptions_app = self.routes