Ruby-on-rails 如何在没有站点布局的情况下呈现 rails 静态 404、500 个错误页面?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9808118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to render rails static 404, 500 error pages without site layout?
提问by istan
I'm using rails 3. In production rails nicely handles exceptions and loads my static 404.html, 500.html etc files from my public directory. However, it loads these files into my layouts/application.html.erb file. I am looking for a way to instruct rails to load these files WITHOUT using my application layout - e.g. just serve the static html file and nothing else. What is the best way to accomplish this?
我正在使用 rails 3。在生产中 rails 很好地处理异常并从我的公共目录加载我的静态 404.html、500.html 等文件。但是,它将这些文件加载到我的 layouts/application.html.erb 文件中。我正在寻找一种方法来指示 rails 在不使用我的应用程序布局的情况下加载这些文件 - 例如,只提供静态 html 文件而不是其他任何东西。实现这一目标的最佳方法是什么?
thanks!
谢谢!
回答by MikDiet
render :file => 'public/404.html', :status => :not_found, :layout => false
render :file => 'public/404.html', :status => :not_found, :layout => false
回答by Shadoath
For an advancedapproach working within the Rails framework. Update your routesfile:
对于在 Rails 框架内工作的高级方法。更新您的routes文件:
get "/404", to: "errors#error_404"
get "/500", to: "errors#error_500"
Add an ErrorsControllerwith:
添加一个ErrorsController:
layout false
def error_404
render status: 404
end
def error_500
render status: 500
end
Then within the app/views/errors/add your error_404.erband error_500.erbfiles along with a snazy imageand a search bar.
然后在里面app/views/errors/添加你的error_404.erb和error_500.erb文件以及一个时髦的图像和一个搜索栏。

