Rails 3 -定制错误和404页面

时间:2022-10-07 14:36:55

I'm building a Rails 3 app on Heroku. Right now my error pages and 404 page are all standard rails/heroku pages.

我正在Heroku上构建一个Rails 3应用程序。现在我的错误页面和404页面都是标准的rails / heroku页面。

I'd like to customize these two. Have a page for an error, and have a page for a 404.

我想定制这两个。有一个错误页面,并有一个404的页面。

All the tutorials found on google are years old, and don't work on Rails 3.

谷歌上发现的所有教程都已有数年之久,并且不适用于Rails 3。

Can you please suggest a good modern tutorial or tips on how to do the above?

你能否提出一个很好的现代教程或如何做到这一点的提示?

Thanks

谢谢

4 个解决方案

#1


36  

If you want to capture specific errors, use rescue_from in ApplicationController.

如果要捕获特定错误,请在ApplicationController中使用rescue_from。

Otherwise if you just want to edit the default error pages, edit the 500.html and 400.html files in {Rails.root}/public

否则,如果您只想编辑默认错误页面,请编辑{Rails.root} / public中的500.html和400.html文件

#2


11  

I found this tutorial quite useful:

我发现本教程非常有用:

http://ramblinglabs.com/blog/2012/01/rails-3-1-adding-custom-404-and-500-error-pages

http://ramblinglabs.com/blog/2012/01/rails-3-1-adding-custom-404-and-500-error-pages

#3


11  

2013 update for Rails 3.2 from Jose Valim

来自Jose Valim的2013 Rails 3.2更新

When Rails 3.0 came out, one of the features that people suddenly missed was the ability to better handle exceptions. The issue was: since Rails 3 became a lot more Rack “fluent”, we had to move some features to the middleware stack and this forced us to move the whole exceptions handling as well. Rails 3.2 attempts to bring some customization back to the game by allowing you to set your own exceptions rack application that is invoked when a failure happens. For instance, you could set the exceptions application to your own router in your config/application.rb:

当Rails 3.0问世时,人们突然想到的一个功能就是能够更好地处理异常。问题是:由于Rails 3变得更加“流畅”,我们不得不将一些功能移到中间件堆栈中,这迫使我们移动整个异常处理。 Rails 3.2尝试通过允许您设置自己的异常机架应用程序(在发生故障时调用)来尝试将某些自定义重新带回游戏。例如,您可以在config / application.rb中将例外应用程序设置为您自己的路由器:

config.exceptions_app = self.routes

Now, every time there is an exception, your router is going to be invoked. Therefore, to render custom 404 pages, you could simply add to your router:

现在,每次出现异常时,都会调用您的路由器。因此,要渲染自定义404页面,您只需添加到路由器:

match "/404", :to => "errors#not_found"

And implement the logic in the controller as you wish! However, there are a few things to keep in mind if you go down this road:

并根据需要在控制器中实现逻辑!但是,如果你走这条路,有几点要记住:

  1. You need to use match in your routes and not get/post/put/delete because such exceptions can happen in any HTTP request;
  2. 您需要在路由中使用匹配而不是get / post / put / delete,因为此类异常可能在任何HTTP请求中发生;
  3. You won’t be able to see your custom exceptions in development unless you set config.consider_all_requests_local to false in your config/environments/development.rb. The reason is, if the request is considered local, Rails will always favor to show the debug exceptions page; [or run rails server -e production]
  4. 除非在config / environments / development.rb中将config.consider_all_requests_local设置为false,否则您将无法在开发中看到自定义异常。原因是,如果请求被认为是本地的,Rails将总是倾向于显示调试异常页面; [或运行rails server -e production]
  5. You can always access the original exception in the controller at env["action_dispatch.exception"];
  6. 您始终可以在env [“action_dispatch.exception”]中访问控制器中的原始异常;
  7. It is not possible to set cookies, the session nor the flash after an exception happens. They all were already serialized back to the client;
  8. 发生异常后,无法设置cookie,会话和闪存。他们都已经被序列化回客户端;
  9. Finally, the default exceptions application used by Rails that simply renders a page in public/STATUS.html is available here: action_dispatch/middleware/public_exceptions.rb
  10. 最后,Rails使用的默认例外应用程序只需在public / STATUS.html中呈现页面即可:action_dispatch / middleware / public_exceptions.rb

Remember that whatever you do in the errors controller, it should not be anything “fancy”. Keep it simple because something already went wrong with your application!

请记住,无论你在错误控制器中做什么,它都不应该是任何“幻想”。保持简单,因为您的应用程序已经出现问题!

#4


3  

Well rails3 still uses the same 404.html, 422.html and 500.html in the public folder. You can customize those.

好吧rails3仍然在公共文件夹中使用相同的404.html,422.html和500.html。您可以自定义这些。

If you're talking about actually catching these exceptions, and doing some dynamic stuff, I think the basic functionality is the same, have some around_filter that catches your particular exception in application_controller ie ActiveRecord::RecordNotFound and do something with that.

如果您正在谈论实际捕获这些异常,并做一些动态的东西,我认为基本功能是相同的,有一些around_filter在application_controller中捕获您的特定异常,即ActiveRecord :: RecordNotFound并执行某些操作。

#1


36  

If you want to capture specific errors, use rescue_from in ApplicationController.

如果要捕获特定错误,请在ApplicationController中使用rescue_from。

Otherwise if you just want to edit the default error pages, edit the 500.html and 400.html files in {Rails.root}/public

否则,如果您只想编辑默认错误页面,请编辑{Rails.root} / public中的500.html和400.html文件

#2


11  

I found this tutorial quite useful:

我发现本教程非常有用:

http://ramblinglabs.com/blog/2012/01/rails-3-1-adding-custom-404-and-500-error-pages

http://ramblinglabs.com/blog/2012/01/rails-3-1-adding-custom-404-and-500-error-pages

#3


11  

2013 update for Rails 3.2 from Jose Valim

来自Jose Valim的2013 Rails 3.2更新

When Rails 3.0 came out, one of the features that people suddenly missed was the ability to better handle exceptions. The issue was: since Rails 3 became a lot more Rack “fluent”, we had to move some features to the middleware stack and this forced us to move the whole exceptions handling as well. Rails 3.2 attempts to bring some customization back to the game by allowing you to set your own exceptions rack application that is invoked when a failure happens. For instance, you could set the exceptions application to your own router in your config/application.rb:

当Rails 3.0问世时,人们突然想到的一个功能就是能够更好地处理异常。问题是:由于Rails 3变得更加“流畅”,我们不得不将一些功能移到中间件堆栈中,这迫使我们移动整个异常处理。 Rails 3.2尝试通过允许您设置自己的异常机架应用程序(在发生故障时调用)来尝试将某些自定义重新带回游戏。例如,您可以在config / application.rb中将例外应用程序设置为您自己的路由器:

config.exceptions_app = self.routes

Now, every time there is an exception, your router is going to be invoked. Therefore, to render custom 404 pages, you could simply add to your router:

现在,每次出现异常时,都会调用您的路由器。因此,要渲染自定义404页面,您只需添加到路由器:

match "/404", :to => "errors#not_found"

And implement the logic in the controller as you wish! However, there are a few things to keep in mind if you go down this road:

并根据需要在控制器中实现逻辑!但是,如果你走这条路,有几点要记住:

  1. You need to use match in your routes and not get/post/put/delete because such exceptions can happen in any HTTP request;
  2. 您需要在路由中使用匹配而不是get / post / put / delete,因为此类异常可能在任何HTTP请求中发生;
  3. You won’t be able to see your custom exceptions in development unless you set config.consider_all_requests_local to false in your config/environments/development.rb. The reason is, if the request is considered local, Rails will always favor to show the debug exceptions page; [or run rails server -e production]
  4. 除非在config / environments / development.rb中将config.consider_all_requests_local设置为false,否则您将无法在开发中看到自定义异常。原因是,如果请求被认为是本地的,Rails将总是倾向于显示调试异常页面; [或运行rails server -e production]
  5. You can always access the original exception in the controller at env["action_dispatch.exception"];
  6. 您始终可以在env [“action_dispatch.exception”]中访问控制器中的原始异常;
  7. It is not possible to set cookies, the session nor the flash after an exception happens. They all were already serialized back to the client;
  8. 发生异常后,无法设置cookie,会话和闪存。他们都已经被序列化回客户端;
  9. Finally, the default exceptions application used by Rails that simply renders a page in public/STATUS.html is available here: action_dispatch/middleware/public_exceptions.rb
  10. 最后,Rails使用的默认例外应用程序只需在public / STATUS.html中呈现页面即可:action_dispatch / middleware / public_exceptions.rb

Remember that whatever you do in the errors controller, it should not be anything “fancy”. Keep it simple because something already went wrong with your application!

请记住,无论你在错误控制器中做什么,它都不应该是任何“幻想”。保持简单,因为您的应用程序已经出现问题!

#4


3  

Well rails3 still uses the same 404.html, 422.html and 500.html in the public folder. You can customize those.

好吧rails3仍然在公共文件夹中使用相同的404.html,422.html和500.html。您可以自定义这些。

If you're talking about actually catching these exceptions, and doing some dynamic stuff, I think the basic functionality is the same, have some around_filter that catches your particular exception in application_controller ie ActiveRecord::RecordNotFound and do something with that.

如果您正在谈论实际捕获这些异常,并做一些动态的东西,我认为基本功能是相同的,有一些around_filter在application_controller中捕获您的特定异常,即ActiveRecord :: RecordNotFound并执行某些操作。