Rails 中的自定义 404 操作

发布于 2024-09-30 22:39:58 字数 72 浏览 5 评论 0原文

默认 Rails 将渲染 404.html,然后它认为错误 404 是合适的。但是,我想通过自定义页面来制作它。请建议正确的方法。

Default Rails will render 404.html, then it thinks an error 404 is appropriate. However, I want make it by custom page. Please suggest the proper way do it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

烂柯人 2024-10-07 22:39:58

这个答案出人意料地难以找到。

如果您想要静态页面,请编辑 404.html。完毕。

如果您想要动态页面,则:

在 config/routes.rb 中:

match '*not_found', to: 'errors#error_404' unless Rails.application.config.consider_all_requests_local

注释掉 unless 子句以在本地开发计算机上测试 404 页面。

生成一堆文件:

rails generate controller errors error_404

编辑 views/errors/error_404.html.erb 进行自定义。

这适用于 Rails 3.1 和 3.2.2。

This answer was surprisingly hard to find.

If you want a static page, edit 404.html. Done.

If you want a dynamic page, then:

in config/routes.rb:

match '*not_found', to: 'errors#error_404' unless Rails.application.config.consider_all_requests_local

Comment out the unless clause to test the 404 page on local dev machine.

Generate a bunch of files:

rails generate controller errors error_404

Edit views/errors/error_404.html.erb to customize.

This works for Rails 3.1 and 3.2.2.

吹梦到西洲 2024-10-07 22:39:58

您可以使用 render_404 猴子修补。

或者,您可以在路由文件底部,转到返回 404 状态代码的操作。

理想情况下,如果您不想在 404 中添加任何自定义代码,您可以只编辑静态 404.html,这是最简单的。

You could use render_404 monkey patching.

Or, you could set a default route at the bottom of your routes file that goes to an action that returns a 404 status code.

Ideally if you do not want any custom code in your 404 you can just edit the static 404.html, which is easiest.

累赘 2024-10-07 22:39:58

根据您的需要,我们的 gem 可能会有所帮助:http://github.com/hedgeyedev/snow

Depending on what you need, our gem might help: http://github.com/hedgeyedev/snow

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文