基本 Rails 404 错误页面 [英] Basic Rails 404 Error Page
问题描述
很长一段时间以来,我一直在寻找一个简单的答案,这似乎必须非常明显和简单,因为没有人有一个简单、愚蠢的证明教程.
I have been looking for a simple answer to this for a ridiculously long time and it seems like this has to be so plainly obvious and simple because no one has an easy, idiot proof tutorial.
无论如何,我想要做的就是拥有一个 404.html 静态页面,只要抛出任何错误,就会加载该页面.理想情况下,这应该只发生在生产和暂存阶段.
Anyway, all I want to do is to have a single 404.html static page that loads whenever ANY error is thrown. Ideally this should only happen in production and staging.
我觉得这应该是最简单的事情……但我想不通.
I feel like this should be the easiest thing to do... but I can't figure it out.
非常感谢任何帮助.
推荐答案
在你的 ApplicationController
unless ActionController::Base.consider_all_requests_local
rescue_from Exception, :with => :render_404
end
private
def render_404
render :template => 'error_pages/404', :layout => false, :status => :not_found
end
现在设置 error_pages/404.html
就可以了
...或者我对 Exception 过于谨慎,你应该从 RuntimeError 中拯救出来.
...or maybe I'm overcautious with Exception and you should rescue from RuntimeError instead.
这篇关于基本 Rails 404 错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!