How to properly render custom 404 and 500 pages?

让人想犯罪 __ 提交于 2019-12-07 01:13:27

Unfortunately there aren't any methods I know of that can be overridden to provide what you want. You could use an around filter. Your code would look something like this:

class ApplicationController < ActionController::Base
  around_filter :catch_exceptions

  protected
    def catch_exceptions
      yield
    rescue => exception
      if exception.is_a?(ActiveRecord::RecordNotFound)
        render_page_not_found
      else
        render_error
      end
    end
end

You can handle each error as you see fit in that method. Then your #render_page_not_found and #render_error methods would have to be something like

render :template => 'errors/404'

You would then need to have a file at app/views/errors/404.html.[haml|erb]

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!