2017-03-17 158 views
3

我是初學者在紅寶石和不能找到解決方案如何在Rails 5中創建自定義404-401頁面。 任何建議嗎? 我已經創建了一個帶有「page_404」動作的控制器「ErrorPages」。 請幫助我。自定義404頁面導軌

回答

2

試試這個

 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 
    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 
+0

不到一分鐘? ....好像你從你的項目粘貼了代碼,因爲它是... –

+0

這是工作,看起來不錯)謝謝 –

+0

你擁有這兩個帳戶? –