【问题标题】:Custom 404 page rails自定义 404 页面导轨
【发布时间】:2017-03-17 10:19:16
【问题描述】:

我是 ruby​​ 的初学者,找不到如何在 Rails 5 中创建自定义 404-401 页面的解决方案。 有什么建议吗? 我用“page_404”操作创建了一个控制器“ErrorPages”。 请帮帮我。

【问题讨论】:

    标签: ruby-on-rails http-status-code-404 custom-pages


    【解决方案1】:

    试试这个:

    class ApplicationController < ActionController::Base
      rescue_from ActiveRecord::RecordNotFound, with: :on_record_not_found
      rescue_from AbstractController::ActionNotFound, with: :on_record_not_found
      rescue_from ActionController::RoutingError, with: :on_routing_error
      rescue_from CanCan::AccessDenied, with: :on_access_denied
    
      def render_404
        if params[:format].present? && params[:format] != 'html'
          head status: 404
        else
          render 'application/404', status: 404
        end
      end
    
      def on_access_denied
        if params[:format].present? && params[:format] != 'html'
          head status: 401
        else
          render 'application/401', status: 401
        end
      end
    
      def on_record_not_found
        render_404
      end
    
      def on_routing_error
        render_404
      end
    end
    

    routes.rb

    Rails.application.routes.draw do
      get '*unmatched_route', :to => 'application#render_404'
    end
    

    /app/views/application/404.html.slim

    .content
     .row
      .col-md-12
        h1 404
    

    【讨论】:

    猜你喜欢
    • 2018-11-16
    • 2010-11-04
    • 2012-10-26
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-01
    相关资源
    最近更新 更多