- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在一个经典的 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,
在 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}
}
关于ruby-on-rails - 自定义错误重定向,闪光通知不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36429153/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!