未定义的方法`每个' for nil:订购方法的NilClass

时间:2017-09-02 20:09:24

标签: ruby-on-rails ruby haml

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

这是我的控制器

 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

1 个答案:

答案 0 :(得分:2)

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
相关问题