订购方法的 nil:NilClass 的未定义方法“each”

undefined method `each' for nil:NilClass for order method

当我尝试 运行 我的代码时出现此错误。根据我的研究,我的问题要么在我的控制器中,要么在我的视图中

这是我的控制器

 class AppointmentsController < ApplicationController
  def index
    @appointments = Appointment.order('appt_time ASC')
    @appointment = Appointment.new
   end
  end

这是我的看法

%h1 React Calendar

%h2 Appointments

%h3 Make a new appointment

= form_for @appointment do |f|
    = f.text_field :title
    = f.text_field :appt_time
    = f.submit 'Make appointment'

= @appointmets.each do |a|
    %h3 = a.title
    %p = a.appt_time

我运行宁

Rails 5.1.3 Ruby 2.4.1

Undefined method <method name> for nil:NilClass 表示您正在调用该方法,在本例中为 .each,针对不存在的对象 (Nil)。

在这种情况下,这只是因为您的视图中有错字,所以变量并不像您认为的那样存在。

= @appointmets.each do |a|
%h3 = a.title
%p = a.appt_time

需要

= @appointments.each do |a|   <--- you missed the "n"
%h3 = a.title
%p = a.appt_time