rails:在 rails 中显示路由错误的用户友好消息 [英] rails: displaying a user friendly message for a routing error in rails

查看:31
本文介绍了rails:在 rails 中显示路由错误的用户友好消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 mac osx leopard 上使用 rails 2.3.每次我输入 Rails 应用程序中不存在的 url 时,我都会收到以下错误

i am working in rails 2.3 on mac osx leopard. every time i type a url that does not exist in a rails application i get the following error

Routing Error

No route matches "/whatever_i_typed" with {:method=>:get}

这是用于开发的发现,但我想知道如何确保用户看到更友好的哎呀!未找到"页面.我正在考虑做一个开始...救援块,但我不知道把它放在哪里,我不知道错误代码(即 ActiveRecord::RecordNotFound)

this is find for development, but i was wondering how i can make sure users see a friendlier 'oops! not found' page. i was thinking about doing a begin...rescue block but i didn't know where to put it, not did i know the error code (i.e ActiveRecord::RecordNotFound)

谢谢!尤瓦尔

推荐答案

这个错误永远不会出现在生产环境中.相反,用户将看到 public/404.html 页面.

This error will never appear in production. Instead, users will see the public/404.html page.

要在您的本地主机上尝试此操作,请将乘客/杂种置于生产模式.在您的 ApplicationController 上覆盖 local_request? 方法,如下所示:

To try this out on your localhost, put passenger/mongrel into production mode. Override the local_request? method on your ApplicationController like so:

class ApplicationController
  def local_request?
    false
  end
end

如果您想尝试动态行为,您可以查看 ActionController 上的 rescue_from 类方法.

If you'd like to experiment with dynamic behavior you can check out the rescue_from class method on ActionController.

这篇关于rails:在 rails 中显示路由错误的用户友好消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆