在开发阶段,如果发生错误时,都会出现错误提示页面,比如:RecordNotFound之类的,虽然这些错误方便开发进行debug,但是等产品上线时,如果还是出现这些页面,对于用户来说是很不友好的。

所以必须定制错误跳转到404和500

下面示范在development下开发的404和500跳转:

首先在 config/environment/development.rb中,找到下面这句代码,将其设为false

config.consider_all_requests_local  = false    # rails 4.0

或者

config.action_controller.consider_all_requests_local = false  # rails 3.0

接着修改route.rb, 在route.rb中增加下面这句:(注意:放到最后一行)

 # make sure this rule is the last one
get '*path' => proc { |env| Rails.env.development? ? (raise ActionController::RoutingError, %{No route matches "#{env["PATH_INFO"]}"}) : ApplicationController.action(:render_not_found).call(env) }

然后在application_controller.rb中增加下面代码:

  def self.rescue_errors
rescue_from Exception, :with => :render_error
rescue_from RuntimeError, :with => :render_error
rescue_from ActiveRecord::RecordNotFound, :with => :render_not_found
rescue_from ActionController::RoutingError, :with => :render_not_found
rescue_from ActionController::UnknownController, :with => :render_not_found
rescue_from ActionController::UnknownAction, :with => :render_not_found
end rescue_errors unless Rails.env.development? def render_not_found(exception = nil)
render :file => "/public/404.html", :status => 404
end def render_error(exception = nil)
render :file => "/public/500.html", :status => 500
end

这样就完成404和500的定制跳转啦! over! @_@!!

注:production环境下的404和500跳转已经自动配置了。

参考链接:

http://www.perfectline.ee/blog/custom-dynamic-error-pages-in-ruby-on-rails

http://chen-miao.iteye.com/blog/1456355

http://www.iteye.com/topic/191531

ruby -- 进阶学习(九)定制错误跳转404和500的更多相关文章

  1. ruby -- 进阶学习(三)Strong Parameters在rail3.0和4.0中的区别

    今天coding的时候遇到一个未知的类型,于是用puts logo_params.class查了下数据类型,然后google了一下发现是 Strong Parameter Strong paramet ...

  2. ruby -- 进阶学习(五)使用Ckeditor插件上传中文图片

    基于rails4.0环境 当使用Ckeditor上传中文命名图片时报错,解决方法是对图片进行重命名 在Ckeditor插件的安装目录下找到controllers/.../application.rb ...

  3. ruby -- 进阶学习(十)自定义路由中:new, :collection和:member的区别

    学习链接:http://rubyer.me/blog/583/ RESTful风格的路由动词默认有7个(分别为:index, show, create, new, edit, update, dest ...

  4. ruby -- 进阶学习(十一)配置解决production环境下无法加载css或js

    最近配置production环境,找了好几份文档,从傻逼到苦逼~~终于配置成功~~@_@!!! 首先,先加载以下几个插件: # Use Uglifier as compressor for JavaS ...

  5. ruby -- 进阶学习(十四)设置background-image(解决无法获取图片路径问题)

    基于rails4.0环境 为了美化界面,添加背景图片,于是又傻逼了一回~~ 一开始在xxx.html.erb中添加:(注:图片的路径为:app/asssets/images/background.jp ...

  6. ruby -- 进阶学习(十七)应用代码优化

    ROR开发,代码优化的方法下面这两项是比较重要的: link_to  Rails的link_to是非常慢的,它的代码实现过于复杂,特别是Rails1.2引入了REST以后,大量的命名路由被使用,这些命 ...

  7. Maven学习(九)-----定制库到Maven本地资源库

    这里有2个案例,需要手动发出Maven命令包括一个 jar 到 Maven 的本地资源库. 要使用的 jar 不存在于 Maven 的中心储存库中. 您创建了一个自定义的 jar ,而另一个 Mave ...

  8. ruby -- 进阶学习(一)subdomain配置与实现

    今天和guanMac童鞋研究的subdomain配置终于有点头绪~~ 之所以会遇到种种难题,个人总结了一下,第一本人太菜,第二英语不好 贴一下guanMac童鞋配置小结的链接:http://my.eo ...

  9. ruby -- 进阶学习(二)paperclip上传图片

    Need to add image attachments to a model? See how with paperclip in this episode. 在命令行输入: rails g pa ...

随机推荐

  1. 转:LIRE的使用

    LIRE的使用:创建索引 LIRE(Lucene Image REtrieval)提供一种的简单方式来创建基于图像特性的Lucene索引.利用该索引就能够构建一个基于内容的图像检索(content- ...

  2. 用Task代替TheadPool

    TheadPool的问题 不支持线程的取消.完成.失败通知等交互性操作 不支持线程执行先后次序 using System; using System.Diagnostics; using System ...

  3. linux C++ 获取文件绝对路径

    提供ftp服务时需要获取文件绝对路径,这里记录一下. #include <stdlib.h> #include <stdio.h> #include <limits.h& ...

  4. Weblogic Session复制策略与方式

    在Weblogic中,HttpSession Replication的方式是通过在weblogic.xml中的session- descriptor的定义persistent-store-type来实 ...

  5. GTD时间管理(3)---行动容器分析理论法

    昨天的一篇文章主要是把清空收集箱的信息,放入对应的清单容器 下面我将会对容器中的每件事经过3个维度的分析 那一个例子直接来说明 Ⅱ:如果这个事件很复杂,需要把这个事件当作一个项目进行拆分,最终量化出完 ...

  6. AppExchange Partner Keynote

    Kick-off Dreamforce at the AppExchange Partner Keynote and hear from industry experts about the tren ...

  7. 如何开发Domino中的WebService

    在domino中写webservice可以使用LotusScript,也可以使用java,由于LotusScript API提供的功能多数都是操作domino数据库中文档的,在web service中 ...

  8. 线程互斥与析构函数中mutex的销毁

    正在实现一个线程池的pthread包装器,突然发现有人在讨论关于http://blog.csdn.net/Solstice/article/details/5238671 是一篇比较老的文章,考虑了下 ...

  9. axis2带list的报文,对象和xml的转换

    import java.util.ArrayList; import java.util.List; import org.apache.log4j.Logger; import org.dom4j. ...

  10. Linux之date

    近期学习需要根据不同的日期的数据,做同样的操作,如果一遍遍的手动操作,太笨重了,有些愚公的味道.所以就想着在shell下,将时间当做变量,然后重复其他的操作. 这里的需求是得到从某天(例如:2014- ...