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

上一篇:eclipse alt+/ 无效时,如何设置 《转》


下一篇:Hibernate从入门到精通(十)多对多单向关联映射