1

我在下拉字段中显示一系列小时

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

我用以下方式渲染数组:

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

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

  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 %>

我做错了什么?

4

2 回答 2

3

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

应该是这样的

  <%= f.select :atime_id, @time_options %>
于 2013-12-04T20:36:22.033 回答
2

在该create方法中,从 the 开始@doctor并沿着链向下:安排,然后params[:appointment],直到找到 nil,然后找出它为什么是 nil。Rails.logger 或 pry 如果您正在运行 rails s 将是您的朋友。

于 2013-02-15T03:35:56.037 回答