ドロップダウンフィールドに時間の配列を表示します
[["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 %>
私が間違っているのは何ですか?