2013-02-14 39 views
1

我在下拉字段中顯示的小時陣列未定義的方法`空?'對於零:NilClass

[["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?' for nil:NilClass` error on:

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

我在做什麼錯了?

+0

是您正確的形式呈現?這聽起來像是因爲你說你正在提交它。你可以發佈你的控制器代碼嗎? – plasticide 2013-02-14 18:52:07

+0

當你*提交*表格?這與*渲染表單非常不同。這是什麼? – 2013-02-14 18:52:32

+0

當我提交表格。 @DaveNewton – evanx 2013-02-14 18:57:45

回答

3

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

應該是這樣的

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

create方法,先從@doctor和下井鏈:日程安排,然後params[:appointment],直到找到零,然後找出它爲什麼是零。 Rails.logger或pry如果你正在運行rails s將會是你的朋友。

相關問題