【发布时间】:2016-04-05 14:23:03
【问题描述】:
在一个经典的 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 通知都可以正常工作)?
【问题讨论】:
-
根据你分享的日志,
Redirected to root_path发生在用户遇到500错误,但在你的日志中却显示404错误。 -
我尝试了 admin_path 和 root_path 甚至其他路径,但同样的事情发生了。实际上只有rescue方法会渲染通知。
-
我正试图弄清楚这一点,因为我也想了解为什么它不通过重定向传递它,等待有人回答它。
-
是的,我也是,这很奇怪,我只是想了解正在发生的事情或我错过了什么。我很确定这是一件小事(一如既往!)
标签: ruby-on-rails