首页 > 系统服务 > 详细

Ubuntu 15.04 Rails4.2.5 处理异常

时间:2016-01-24 19:28:54      阅读:205      评论:0      收藏:0      [点我收藏+]

1. 修改: /app/controllers/application_controller.rb文件为如下样子:

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

 #以下为本次添加内容

  rescue_from Exception, :with => :handle_exception
  def not_found
   render :template => "shared/not_found", :status => 404
  end
private
 def handle_exception(exception)
#  case exception
#   when CanCan::AccessDenied
#    authenticate_user!
#   when ActiveRecord::RecordNotFound
#    not_found
#   else
    internal_server_error(exception)
#   end
 end
 def internal_server_error(exception)
  render :file => "public/500.html", :status =>500, :layout=>false
 end
end
2. 修改 config/routes.rb 文件,把其最后一行变成:
 match "*any", :to =>"application#not_found", via: :all
end

3. 完成.

 

Ubuntu 15.04 Rails4.2.5 处理异常

原文:http://www.cnblogs.com/agnt/p/5155586.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!