Ruby on rails 如何自定义我的500.html错误页面?(轨道)
至此,我已经完成了以下工作(请参见下面的代码),404.html.erb页面工作正常!但是,由于某些原因,500.html.erb页面没有加载,它会一直默认返回公用文件夹中的500.html页面。所以我删除了这个500.html文件,现在当出现异常时,它只是一个空白的白色屏幕 我做错了什么?为什么自定义404页面可以工作?(使用rails 4 btw) 请参见下面的我的代码: 错误\u controller.rbRuby on rails 如何自定义我的500.html错误页面?(轨道),ruby-on-rails,ruby-on-rails-4,Ruby On Rails,Ruby On Rails 4,至此,我已经完成了以下工作(请参见下面的代码),404.html.erb页面工作正常!但是,由于某些原因,500.html.erb页面没有加载,它会一直默认返回公用文件夹中的500.html页面。所以我删除了这个500.html文件,现在当出现异常时,它只是一个空白的白色屏幕 我做错了什么?为什么自定义404页面可以工作?(使用rails 4 btw) 请参见下面的我的代码: 错误\u controller.rb class ErrorsController < ApplicationCo
class ErrorsController < ApplicationController
def error_404
@not_found_path = params[:not_found]
end
def error_500
end
end
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
<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>
类错误控制器
应用程序\u控制器.rb
class ErrorsController < ApplicationController
def error_404
@not_found_path = params[:not_found]
end
def error_500
end
end
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
<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>
class ApplicationController
视图/errors/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>
哎呀!找不到此页。
查看/errors/error_500.html.erb
class ErrorsController < ApplicationController
def error_404
@not_found_path = params[:not_found]
end
def error_500
end
end
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
<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?首先在
routes.rb中写入错误路由
get "500", to "errors#error_500", code: "500"
然后使用相应的控制器方法和模板进行处理。在应用程序控制器中调用渲染模板:“errors/error_35;{status}”
,并且与errors_控制器中的操作名称相匹配,但视图文件未命名为“error_404.html.erb”和“error_500.html.erb”。我看不出这两个页面是如何以这种方式正确呈现的。也许我刚刚度过了漫长的一天。另外,application_controller.rb中的format.all{render nothing:true,status:status
行在末尾缺少一个}
。在Rails之外可能会发生500个错误。。。您可能希望生成这些页面并将其缓存到public/500.html等中,然后配置web服务器(nginx/apache)以使用它们。。。