We have a rails server with custom 404 and 500 pages setup using this tutorial here:
我们在这里使用本教程设置了自定义404和500页的rails服务器:
http://ramblinglabs.com/blog/2012/01/rails-3-1-adding-custom-404-and-500-error-pages
http://ramblinglabs.com/blog/2012/01/rails-3-1-adding-custom-404-and-500-error-pages
While it works nice and throws 404s for all kinds of paths, it generates internal server errors 500 while trying to access any kind of suffixed path like en/foo.png, en/foo.pdf, en/foo.xml, ...
虽然它很好用并且为所有类型的路径抛出404,但它在尝试访问任何类型的后缀路径时会产生内部服务器错误500,如en / foo.png,en / foo.pdf,en / foo.xml,...
But something like en/file.foo throws 404. So only valid suffixes throw a 500.
但是像en / file.foo这样的东西会抛出404.所以只有有效的后缀会抛出500。
End of routes.rb:
routes.rb结束:
if Rails.application.config.consider_all_requests_local
match '*not_found', to: 'errors#error_404'
end
application_controller.rb
application_controller.rb
unless Rails.application.config.consider_all_requests_local
rescue_from Exception, with: :render_500
rescue_from ActionController::RoutingError, with: :render_404
rescue_from ActionController::UnknownController, with: :render_404
rescue_from ::AbstractController::ActionNotFound, with: :render_404
rescue_from ActiveRecord::RecordNotFound, with: :render_404
end
protected
def render_404(exception)
@not_found_path = exception.message
respond_to do |format|
format.html { render template: 'errors/error_404', layout: 'layouts/application', status: 404 }
format.all { render nothing: true, status: 404 }
end
end
def render_500(exception)
logger.fatal(exception)
respond_to do |format|
format.html { render template: 'errors/error_500', layout: 'layouts/application', status: 500 }
format.all { render nothing: true, status: 500}
end
end
500 that appears:
500出现:
Missing template errors/error_404 with {:locale=>[:de, :en], :formats=>[:png], :handlers=>[:erb, :builder, :coffee, :arb, :haml]}
2 个解决方案
#1
13
We found the mistake.
我们发现了错误。
We had an error_controller.rb containing this:
我们有一个error_controller.rb包含这个:
def error_404
@not_found_path = params[:not_found]
render template: 'errors/error_404', layout: 'layouts/application', status: 404
end
and we changed it to fix this problem to:
我们更改它以解决此问题:
def error_404
@not_found_path = params[:not_found]
respond_to do |format|
format.html { render template: 'errors/error_404', layout: 'layouts/application', status: 404 }
format.all { render nothing: true, status: 404 }
end
end
#2
1
Try adding
尝试添加
respond_to :html, :json, :png
respond_to:html,:json,:png
and any other necessary formats at the top of your controller. If I'm right, then the problem is that format.all
in the individual controller actions isn't set up to include :png
as one of the formats it responds to.
以及控制器顶部的任何其他必要格式。如果我是对的,那么问题是单个控制器动作中的format.all没有设置为包括:png作为它响应的格式之一。
You will probably also need to add to your config/environment.rb
the following definition and any similar ones:
您可能还需要在config / environment.rb中添加以下定义和任何类似的定义:
Mime::Type.register "image/png", :png
Mime :: Type.register“image / png”,:png
See more details here. Basically you need to set up the mime types that you want to respond to. The error message indicates that rails doesn't understand how to render the format png
.
在这里查看更多细节。基本上,您需要设置要响应的mime类型。错误消息表明rails不了解如何呈现格式png。
#1
13
We found the mistake.
我们发现了错误。
We had an error_controller.rb containing this:
我们有一个error_controller.rb包含这个:
def error_404
@not_found_path = params[:not_found]
render template: 'errors/error_404', layout: 'layouts/application', status: 404
end
and we changed it to fix this problem to:
我们更改它以解决此问题:
def error_404
@not_found_path = params[:not_found]
respond_to do |format|
format.html { render template: 'errors/error_404', layout: 'layouts/application', status: 404 }
format.all { render nothing: true, status: 404 }
end
end
#2
1
Try adding
尝试添加
respond_to :html, :json, :png
respond_to:html,:json,:png
and any other necessary formats at the top of your controller. If I'm right, then the problem is that format.all
in the individual controller actions isn't set up to include :png
as one of the formats it responds to.
以及控制器顶部的任何其他必要格式。如果我是对的,那么问题是单个控制器动作中的format.all没有设置为包括:png作为它响应的格式之一。
You will probably also need to add to your config/environment.rb
the following definition and any similar ones:
您可能还需要在config / environment.rb中添加以下定义和任何类似的定义:
Mime::Type.register "image/png", :png
Mime :: Type.register“image / png”,:png
See more details here. Basically you need to set up the mime types that you want to respond to. The error message indicates that rails doesn't understand how to render the format png
.
在这里查看更多细节。基本上,您需要设置要响应的mime类型。错误消息表明rails不了解如何呈现格式png。