我在下拉字段中顯示的小時陣列未定義的方法`空?'對於零: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 %>
我在做什麼錯了?
是您正確的形式呈現?這聽起來像是因爲你說你正在提交它。你可以發佈你的控制器代碼嗎? – plasticide 2013-02-14 18:52:07
當你*提交*表格?這與*渲染表單非常不同。這是什麼? – 2013-02-14 18:52:32
當我提交表格。 @DaveNewton – evanx 2013-02-14 18:57:45