Rails 中的自定义 404 操作
默认 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这个答案出人意料地难以找到。
如果您想要静态页面,请编辑
404.html
。完毕。如果您想要动态页面,则:
在 config/routes.rb 中:
注释掉
unless
子句以在本地开发计算机上测试 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:
Comment out the
unless
clause to test the 404 page on local dev machine.Generate a bunch of files:
Edit
views/errors/error_404.html.erb
to customize.This works for Rails 3.1 and 3.2.2.
您可以使用 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.
根据您的需要,我们的 gem 可能会有所帮助:http://github.com/hedgeyedev/snow
Depending on what you need, our gem might help: http://github.com/hedgeyedev/snow