0
所以,我已經做了以下幾點(見下面的代碼)和404.html.erb頁面工作得很好!但是,由於某些原因,500 .html.erb頁面不會加載,因此它會保持默認回到公用文件夾中的500.html頁面。所以我刪除了這個500.html文件,現在當出現異常時,它只是一個空白的白屏。如何自定義我的500.html錯誤頁面? (Rails)
我在做什麼錯?爲什麼自定義404頁面可以工作? (使用Rails 4 BTW)
見下面我的代碼:
errors_controller.rb
class ErrorsController < ApplicationController
def error_404
@not_found_path = params[:not_found]
end
def error_500
end
end
application_controller.rb
class ApplicationController < ActionController::Base
...
unless Rails.application.config.consider_all_requests_local
rescue_from Exception, with: lambda { |exception| render_error 500, exception }
rescue_from ActionController::RoutingError, ActionController::UnknownController, ::AbstractController::ActionNotFound, ActiveRecord::RecordNotFound, with: lambda { |exception| render_error 404, exception }
end
...
private
def render_error(status, exception)
respond_to do |format|
format.html { render template: "errors/error_#{status}", layout: 'layouts/application', status: status }
format.all { render nothing: true, status: status }
end
end
的意見/錯誤/ error_404。 html.erb:
<div class="errorbox">
<h1><%= image_tag('errorimage.png') %>Oops! This page couldn't be found.</h1>
<p class="leftbox"><%= link_to 'Return to previous page', :back %></p>
<p><%= link_to 'Report a problem', contact_us_path %></p>
</div>
的意見/錯誤/ error_500.html.erb
<div class="errorbox">
<h1><%= image_tag('errorimage.png') %>Oops! Something went wrong.</h1>
<p class="leftbox"><%= link_to 'Return to previous page', :back %></p>
<p><%= link_to 'Report a problem', contact_us_path %></p>
</div>
我怎樣才能獲得時,有一個服務器錯誤500.html.erb要顯示的習慣嗎?
您在應用程序控制器中調用了'render template:「errors/error _#{status}」',並且與errors_controller中的操作名稱相匹配,但您的視圖文件未被命名爲「error_404.html.erb」和「 error_500.html.erb」。我看不出這兩個頁面是如何正確渲染的。也許我剛剛度過了漫長的一天。 此外,application_controller.rb中的'format.all {render nothing:true,status:status'行在結尾處缺少'}'。 – digijim 2014-09-18 22:00:56
500錯誤可能發生在Rails之外......您可能想要生成這些頁面並將其緩存到public/500.html等,並配置您的Web服務器(nginx/apache)以使用它們... – 2014-09-18 23:54:02