ruby-on-rails - 用于 nil:NilClass 的 Rails 未定义方法“each”仅在生产中
问题描述
刚刚使用 Capistrano 在 Ubuntu 20.04.1 LTS 上部署了我的 Rails 项目。配置 MongoDB 数据库,使用 rails 控制台获取一些记录没有问题。在本地也可以正常工作。但我收到关于 nil 收集的错误。我认为我缺少一些具体的东西。
有什么解决办法吗?
控制器:
class InstitutionsController < ApplicationController
def index
@presenter = Institutions::IndexPresenter.new(Institution.all)
end
end
主持人:
module Institutions
class IndexPresenter < ApplicationPresenter
attr_reader :institutions
def initialize(institutions)
@institutions = institutions
end
end
end
index.html.slim
.row
- if @presenter.institutions.present?
- @presenter.institutions.each do |institution|
.col-lg-4.col-sm-6.mb-4
.card.h-100
a href="#{institution_path(institution)}"
img.card-img-top alt="" src="my-image"
.card-body
h3.card-title
a
= institution.name
- else
.alert.alert-danger role="alert"
| No instances!
机构模式
class Institution
include Mongoid::Document
field :name, type: String
has_many :rooms, dependent: :destroy, class_name: 'Institutions::Room'
validates :name, presence: true
end
解决方案
推荐阅读
- leaflet - 如何将嵌套多边形与来自不同 GeoJSON 文件的父级相关联?
- docker - 如何在 docker 容器中调试“没有路由到主机”
- react-leaflet - 瓷砖不好取
- bash - 在给定时间后杀死 nohup 进程
- javascript - 使用隐藏的 WebView 并在 Javascript 和 Java 代码之间调用函数
- javascript - 如何为 Stampit 对象自动创建 TypeScript 或 Flow 定义?
- php - 有什么作用?在 PHP 中代表(在 href 运算符之后)?
- python - 如何以可以像在fortran中一样将连续零写为n * 0的格式声明数组?
- javascript - 从 URL 数据中获取并转换为 JSON 数组
- javascript - 如何在打字稿中定义计算属性