ruby -- 进阶学习(九)定制错误跳转404和500
在开发阶段,如果发生错误时,都会出现错误提示页面,比如: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的更多相关文章
- ruby -- 进阶学习(三)Strong Parameters在rail3.0和4.0中的区别
今天coding的时候遇到一个未知的类型,于是用puts logo_params.class查了下数据类型,然后google了一下发现是 Strong Parameter Strong paramet ...
- ruby -- 进阶学习(五)使用Ckeditor插件上传中文图片
基于rails4.0环境 当使用Ckeditor上传中文命名图片时报错,解决方法是对图片进行重命名 在Ckeditor插件的安装目录下找到controllers/.../application.rb ...
- ruby -- 进阶学习(十)自定义路由中:new, :collection和:member的区别
学习链接:http://rubyer.me/blog/583/ RESTful风格的路由动词默认有7个(分别为:index, show, create, new, edit, update, dest ...
- ruby -- 进阶学习(十一)配置解决production环境下无法加载css或js
最近配置production环境,找了好几份文档,从傻逼到苦逼~~终于配置成功~~@_@!!! 首先,先加载以下几个插件: # Use Uglifier as compressor for JavaS ...
- ruby -- 进阶学习(十四)设置background-image(解决无法获取图片路径问题)
基于rails4.0环境 为了美化界面,添加背景图片,于是又傻逼了一回~~ 一开始在xxx.html.erb中添加:(注:图片的路径为:app/asssets/images/background.jp ...
- ruby -- 进阶学习(十七)应用代码优化
ROR开发,代码优化的方法下面这两项是比较重要的: link_to Rails的link_to是非常慢的,它的代码实现过于复杂,特别是Rails1.2引入了REST以后,大量的命名路由被使用,这些命 ...
- Maven学习(九)-----定制库到Maven本地资源库
这里有2个案例,需要手动发出Maven命令包括一个 jar 到 Maven 的本地资源库. 要使用的 jar 不存在于 Maven 的中心储存库中. 您创建了一个自定义的 jar ,而另一个 Mave ...
- ruby -- 进阶学习(一)subdomain配置与实现
今天和guanMac童鞋研究的subdomain配置终于有点头绪~~ 之所以会遇到种种难题,个人总结了一下,第一本人太菜,第二英语不好 贴一下guanMac童鞋配置小结的链接:http://my.eo ...
- ruby -- 进阶学习(二)paperclip上传图片
Need to add image attachments to a model? See how with paperclip in this episode. 在命令行输入: rails g pa ...
随机推荐
- ooj1057: M的整数倍DP
http://121.249.217.157/JudgeOnline/problem.php?id=1057 1057: M的整数倍 时间限制: 1 Sec 内存限制: 64 MB提交: 130 ...
- 智能配置item
iTerm配置说明 ==== **这是一个很智能的配置,不论你电脑里面有没有安装iTerm,不管你有没有.zshrc配置文件,git配置文件,git忽略文件,以及git的默认编辑器都已经更改为vim, ...
- 攻城利器 —— Carthage简单介绍
01 Carthage简单介绍 主页:https://github.com/Carthage/Carthage.git 作者:Justin Spahr-Summers等 版本:0.18 目标:用最简单 ...
- Intrinsics头文件与SIMD指令集、Visual Studio版本对应表(转)
File:Intrinsics头文件 描述:指令集描述VS:Visual Studio版本号VisualStudio:Visual Studio版本名 File 描述 VS VisualStudio ...
- DIV实现CSS 的placeholder效果
placeholder是HTML5中input的属性,但该属性并不支持除input以外的元素 但我们可以使用Css before选择器来实现完全相同的效果 <!DOCTYPE html> ...
- java 反射: 当Timestamp类型的属性值为null时,设置默认值
import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.lang.reflect.Metho ...
- 关于Server Sql 2008触发器的使用
1.创建同一个服务器下同一个数据库实例两个不同数据库表同步方法 --==================================== -- Create database trigger -- ...
- 【转】开放api接口签名验证
不要急,源代码分享在最底部,先问大家一个问题,你在写开放的API接口时是如何保证数据的安全性的?先来看看有哪些安全性问题在开放的api接口中,我们通过http Post或者Get方式请求服务器的时候, ...
- js去掉html标签和去掉字符串文本的所有的空格
去掉html标签的js <script> function delHtmlTag(str){ return str.replace(/<[^>]+>/g,"& ...
- 如何升级TeX Live 2014宏包
转:人大经济论坛 LATEX论坛 版,详细出处参考: http://bbs.pinggu.org/forum.php?mod=viewthread&tid=3370640&page=1 ...