【问题标题】:Rails 4 custom 404 causes postgresql connection failure on HerokuRails 4 自定义 404 导致 Heroku 上的 postgresql 连接失败
【发布时间】:2014-02-17 16:31:59
【问题描述】:

我在 Heroku 上部署了一个带有自定义域的 Rails 4 应用程序。我也有一个临时版本。该应用使用舒适的墨西哥沙发。

出现以下问题:应用程序将达到所有请求都返回 500 错误的状态。日志显示:

[jesse@Athens expat]$ heroku logs
ActiveRecord::ConnectionTimeoutError (could not obtain a database connection within 5.000 seconds (waited 5.000 seconds)):

[jesse@Athens expat]$ heroku pg:info
Connections: 5

[jesse@Athens expat]$ heroku pg:ps
 pid | state | source | running_for | waiting | query 
-----+-------+--------+-------------+---------+-------
(0 rows)

[jesse@Athens expat]$ heroku pg:killall
 pg_terminate_backend 
----------------------
 t
 t
 t
 t
 t
(5 rows)

随后尝试连接会导致 500 错误,并且 db 连接保持在 0。

在我使用本指南创建自定义错误页面后出现此问题:http://wearestac.com/blog/dynamic-error-pages-in-rails

我可以通过创建一个使用数据库的 404 页面然后向服务器发出大约 5 或 6 个请求以获取不存在的页面来强制解决此问题。

编辑:

我可以在使用静态自定义 404 页面时强制解决问题,方法是发出 5 或 6 个带有 jpeg 扩展名的不存在文件的请求。日志中出现以下内容:

Error during failsafe response: Missing template errors/not_found, application/not_found with {:locale=>[:en], :formats=>[:jpeg], :handlers=>[:erb, :builder, :raw, :ruby, :jbuilder, :coffee, :haml]}. Searched in:
2014-02-17T17:26:14.595469+00:00 app[web.1]:   * "/app/app/vi
ews"
2014-02-17T17:26:14.594508+00:00 app[web.1]: Completed 500 Internal Server Error in 7ms

我们将不胜感激任何隔离此问题的帮助。提前致谢

【问题讨论】:

  • 它正在尝试以 404 响应 jpeg 图像。所以它正在寻找 not_found.jpg.erb 模板。显然你没有那个。我不知道 Heroku 是如何做到这一点的细节,但你真的想防止它在提供静态内容(如图像、css、js 等)时碰到 Rails 堆栈。
  • 啊,你可能想安装这个:github.com/heroku/rails_serve_static_assets
  • 谢谢杂货店。在 respond_to 块中处理不同的格式就可以了。
  • 您能否将解决方案移至答案,使其显示为已回答?

标签: ruby-on-rails postgresql heroku http-status-code-404 comfortable-mexican-sofa


【解决方案1】:

这里是功能控制器代码:

class ErrorsController < ApplicationController
  def not_found
    respond_to do |format|
      format.any(:htm, :html, :xls, :xlsx) { render :status => 404, :layout => "error_frame", :formats => [:html] }
      format.all { render nothing: true, status: 404 }
    end
  end

  def unacceptable
    respond_to do |format|
      format.html { render :status => 422, :layout => "error_frame" }
      format.all { render nothing: true, status: 422 }
    end
  end

  def internal_error
    respond_to do |format|
      format.html { render :layout => false, :status => 500 }
      format.all { render nothing: true, status: 500}
    end
  end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    • 2013-09-06
    • 1970-01-01
    相关资源
    最近更新 更多