我刚开始设置 VPS。
今晚早些时候,我使用 Ubuntu、Postgresql、Nginx、Passenger 设置了一个 VPS。
在 APP 崩溃之前一切顺利,现在我在刷新页面时总是收到 Sorry, something wrong wrong
消息。
我已经使用 sudo service nginx restart
重启了 NginX
我还重新启动了 postgresql
。
我还一次又一次地部署
应用程序,但都没有成功。
在production.log
有这样的消息:
NoMethodError (undefined method '+' for nil:NilClass):
app/controllers/application_controller.rb:101:in `each'
app/controllers/application_controller.rb:101:in `reduce'
app/controllers/application_controller.rb:101:in `users_stats'
现在这个错误总是出现。即使在所有重新启动之后。
更新
这是代码,我设法通过在 Controller 中对其进行注释并再次部署使其工作。
all_users_truck_use = User.joins(:transports).where(transports: { transport_type: ['Truck / Lorry', nil] } ).pluck(:transport_km).reduce(:+)
@all_users_Truck_co2 = all_users_truck_use.nil? ? 0 : all_users_truck_use * @Truck.to_f
服务器是否进入某种循环或? 我真的很绝望,我需要在星期一早上交付这个项目。
提前致谢
最佳答案
错误消息 NoMethodError (undefined method '+' for nil:NilClass)
告诉您您尝试在 nil
上调用 +
。如果您执行类似 nil + 1
的操作,就会发生该错误,而 1 + nil
会引发不同的错误。
并且我假设以下行(为了便于阅读而重新格式化)是 application_controller.rb
中的行 101
all_users_truck_use = User
.joins(:transports)
.where(transports: { transport_type: ['Truck / Lorry', nil] } )
.pluck(:transport_km)
.reduce(:+)
来自 reduce
的文档:
If you do not explicitly specify an initial value for memo, then the first element of collection is used as the initial value of memo.
这意味着 User.joins(:transports).where(transports: { transport_type: ['Truck/Lorry', nil] }).pluck(:transport_km)
返回的第一个元素必须为 nil
。
一个非常简单的解决方案可能是通过在 reduce
之前调用 compact
从该列表中删除 nil
值:
all_users_truck_use = User
.joins(:transports)
.where(transports: { transport_type: ['Truck / Lorry', nil] } )
.pluck(:transport_km)
.compact
.reduce(:+)
更好的解决方案可能是添加验证以确保所有数据库记录都具有有效的 transport_km
并清理现有记录。或者完全在 SQL
中进行计算。如果没有更多信息,很难说出您的数据库架构如何加载、数据来自何处以及该行的上下文及其用例。
关于ruby-on-rails - 我使用 Nginx 在 Ubuntu 上运行的 VPS 崩溃了,无法再次运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46135594/