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 ...
随机推荐
- Python之Web框架们
Python的WEB框架 Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Python的标准库外,其不依赖任何其他模块. pip i ...
- DOM基本概念和操作
1.基本概念 DOM是文档对象模型(TEXT),对象是指文档中的每一个元素. 2.Window对象操作 打开方式: _blank 在新窗口还是自身窗口. Window.open 也有返回值,返回值为 ...
- 使用 multiprocessing.dummy 执行多线程任务
# -*- coding: utf-8 -*- # from multiprocessing import Pool 多进程 from multiprocessing.dummy import Poo ...
- C#Winform程序如何发布并自动升级(图解)
C#Winform程序如何发布并自动升级(图解) 有不少朋友问到C#Winform程序怎么样配置升级,怎么样打包,怎么样发布的,在这里我解释一下打包和发布 关于打包的大家可以看我的文章C# w ...
- table表格宽带研究(附带:table表格为什么设置td宽度无效)
情况1:下面代码定义了表格宽带为为600px,未设置td宽带,3个td内容为1,2,3,可以看到3个内容平分table的宽度. 也就是每个td都是200px(请注意:如果用chrome调试宽度,会有一 ...
- Android的消息机制: Message/MessageQueue/Handler/Looper
概览 * Message:消息.消息里面可包含简单数据.Object和Bundle,还可以包含一个Runnable(实际上可看做回调). * MessageQueue:消息队列,供Looper线程 ...
- How to programmatically new a java class which implements sepecified interface in eclipse plugin development
http://w3facility.org/question/how-to-programmatically-new-a-java-class-which-implements-sepecified- ...
- 启动和JQuery绑定--AngularJS学习笔记(二)
上一篇简单的分析了AngularJS的项目结构,后面就开始分析具体的源代码了. 从angularFiles.js中的定义可以看出有几个文件直接位于src根目录,并不是隶属于某个模块.这几 个分别是mi ...
- The Truth About .NET Objects And Sharing Them Between AppDomains
From http://geekswithblogs.net/akraus1/archive/2012/07/25/150301.aspx I have written already some ti ...
- jQuery File Upload跨域上传
最近在做一个一手粮互联网项目,方案为前后端分离,自己负责前端框架,采用了Requirejs+avalonjs+jquery三个框架完成. 前后端通过跨域实现接口调用,中间也发现了不少问题,尤其是在富文 ...