未定义的方法`空?’ 为零:NilClass

我在下拉字段中显示一个小时数组

[["09:30am", "2013-02-14 09:30:00"], ["10:00am", "2013-02-14 10:00:00"] ... 

我渲染数组:

  

我将数组的值赋给控制器中的实例变量

  def new @time_options = Appointment.time_options @doctor = Doctor.find(params[:doctor_id]) @appointment = @doctor.schedule.appointments.build end # GET /appointments/1/edit def edit @time_options = Appointment.time_options @doctor = Doctor.find(params[:doctor_id]) @appointment = @doctor.appointments.find(params[:id]) end # POST /appointments # POST /appointments.json def create @time_options = Appointment.time_options @doctor = Doctor.find(params[:doctor_id]) @appointment = @doctor.schedule.appointments.new(params[:appointment]) if @appointment.save #send email AppointmentMailer.appointment_confirmation(@appointment, I18n.locale).deliver AppointmentMailer.new_appointment(@appointment, I18n.locale).deliver redirect_to :action => 'thank_you' else render action: 'new' end end 

在模型中,我定义了一个类方法来构建数组

  def self.time_options start_of_day = Time.zone.now.beginning_of_day start_time = start_of_day + 9.hours end_time = start_of_day + 17.hours lunch_start = start_of_day + 13.hours lunch_end = start_of_day + 14.hours + 30.minutes times = [] until start_time > end_time start_time = (start_time + 30.minutes) unless start_time >= lunch_start && start_time < lunch_end times << start_time end end times.map{|t| [t.strftime("%I:%M%p").downcase, t.to_s(:db)] } end 

但是,当我提交表单时,我得到一个undefined method空? 对于nil:NilClass`错误:

  

我做错了什么?

这个

  <%= f.select :atime, @time_options %> 

应该是这样的

  <%= f.select :atime_id, @time_options %> 

create方法中,从@doctor开始,然后沿着chain:schedule,然后params[:appointment] ,直到找到nil,然后弄清楚为什么它是nil。 Rails.logger或pry,如果你正在运行rails s将是你的朋友。