Ubuntu 15.04 Rails4.2.5 处理异常

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. 完成.

 

posted @ 2016-01-24 18:22  戈_戈  阅读(168)  评论(0编辑  收藏  举报