Ruby-on-rails 基本 Rails 404 错误页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1447627/
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
Basic Rails 404 Error Page
提问by chrishomer
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.
很长一段时间以来,我一直在寻找一个简单的答案,这似乎必须非常明显和简单,因为没有人有一个简单的白痴证明教程。
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.
无论如何,我想要做的就是拥有一个 404.html 静态页面,每当抛出任何错误时都会加载该页面。理想情况下,这应该只发生在生产和登台中。
I feel like this should be the easiest thing to do... but I can't figure it out.
我觉得这应该是最简单的事情……但我想不通。
Any help is much appreciated.
任何帮助深表感谢。
采纳答案by Leonid Shevtsov
in your ApplicationController
在你的 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
now set up error_pages/404.htmland there you go
现在设置好error_pages/404.html就可以了
...or maybe I'm overcautious with Exception and you should rescue from RuntimeError instead.
...或者我对 Exception 过于谨慎,你应该从 RuntimeError 中拯救出来。
回答by Sophie Alpert
I believe that if you run in production mode, then 404.html in the public directory gets served whenever there are no routes for a URL.
我相信,如果您在生产模式下运行,那么只要 URL 没有路由,就会提供公共目录中的 404.html。
回答by Manish Shrivastava
If you run in production mode, 404.html,500.html,422.html files in public directory gets served whenever there respective error occured, pages from above will be shown.
如果您在生产模式下运行,则在发生相应错误时会提供公共目录中的 404.html,500.html,422.html 文件,将显示上面的页面。
In rails 3.1
在轨道 3.1
We can use like below: Rails 3.1 will automatically generate a response with the correct HTTP status code (in most cases, this is 200 OK). You can use the :status option to change this:
我们可以像下面这样使用:Rails 3.1 会自动生成一个带有正确 HTTP 状态码的响应(在大多数情况下,这是 200 OK)。你可以使用 :status 选项来改变它:
render :status => 500
render :status => :forbidden
渲染:状态 => 500
渲染:状态=>:禁止
Rails understands both numeric and symbolic status codes.
Rails understands both numeric and symbolic status codes.
Fore more information see this page
Cheers!
干杯!
回答by theIV
You won't get a 404 whenever any error is thrown because not all errors result in 404s. That's why you have 404, 422, and 500 pages in your public directory. I guess rails has deemed these to be the most common errors. Like Ben said, 404 will come up when it can't find something, 500 when the application throws an error. Between the two, you can cover a lot of your bases.
无论何时抛出任何错误,您都不会得到 404,因为并非所有错误都会导致 404。这就是为什么您的公共目录中有 404、422 和 500 页的原因。我猜 Rails 认为这些是最常见的错误。就像 Ben 说的那样,当它找不到某些东西时会出现 404,当应用程序抛出错误时会出现 500。在两者之间,您可以涵盖很多基础。
回答by Bruno Buccolo
Another way of doing this is configuring your config/application.rbwith the following:
另一种方法是config/application.rb使用以下内容配置您:
module YourApp
class Application < Rails::Application
# ...
config.action_dispatch.rescue_responses.merge!(
'MyCustomException' => :not_found
)
end
end
So that whenever MyCustomExceptionis raised, Rails treats it as a regular :not_found, rendering public/404.html.
因此,无论何时MyCustomException引发,Rails 都会将其视为常规的:not_found呈现public/404.html。
To test this locally, make sure you change config/environments/development.rbto:
要在本地进行测试,请确保更改config/environments/development.rb为:
config.consider_all_requests_local = false
Read more about config.action_dispatch.rescue_responses.
回答by Jason Swett
Here's how I do it. In my application_controller.rb:
这是我如何做到的。在我的application_controller.rb:
def render_404
render file: 'public/404.html', layout: false, status: :not_found
end
Then, in any controller where I want to render a 404, I do something like this:
然后,在我想要渲染 404 的任何控制器中,我执行以下操作:
@appointment = Appointment.find_by(id: params[:appointment_id]) || render_404

