首页 > 解决方案 > 用于 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

最后,我收到 nil 收集的随机错误 在此处输入图像描述

也用于特定的记录显示。同样的错误。 在此处输入图像描述

标签: ruby-on-rails

解决方案


推荐阅读