红联Linux门户
Linux帮助

Ubuntu 15.04 Rails4.2.5处理异常

发布时间:2016-01-25 09:29:39来源:linux网站作者:戈_戈

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


本文永久更新地址:http://www.linuxdiyf.com/linux/17642.html