chr*_*mer 25 routing ruby-on-rails http-status-code-404
我一直在寻找一个简单的答案,这是一个非常漫长的时间,似乎这一点非常明显和简单,因为没有人有一个简单的,白痴证明教程.
无论如何,我想做的就是有一个单独的404.html静态页面,无论何时抛出任何错误都会加载.理想情况下,这应该仅在生产和分期中发生.
我觉得这应该是最简单的事情......但我无法弄明白.
任何帮助深表感谢.
Leo*_*sov 20
在你的 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
Run Code Online (Sandbox Code Playgroud)
现在设置,error_pages/404.html然后你去
...或者我对Exception过于谨慎,你应该从RuntimeError中解救出来.
如果您在生产模式下运行,则只要出现相应的错误,就会在公共目录中提供404.html,500.html,422.html文件,将显示上面的页面.
在轨道3.1
我们可以使用如下:Rails 3.1将自动生成具有正确HTTP状态代码的响应(在大多数情况下,这是200 OK).您可以使用:status选项更改此设置:
渲染:status => 500
render:status =>:禁止
Rails understands both numeric and symbolic status codes.
干杯!