I have been searching the solution whole day today, but unable to find out. This is a simple appointment system. I try to show the error message and render to 'new' when the user submit invalid date/time(eg: Sat, Sun / not range between 09:00-17:00). I keep getting the error "First argument in form cannot contain nil or be empty" when the condition is falling into the validation. Also, when its not in invalid condition, the flash message is not appearing, but redirect to the desired page.
In _form.html.erb
<%= form_for @appointment do |f| %>
<%= f.date_field :appointment_date, :min => Date.today %>
<%= time_field_tag 'appointment[appointment_time]' %>
<%= f.submit "Submit"%>
<% end %>
In Appointment controller (class AppointmentsController < ApplicationController)
def create
appointment = Appointment.new appointment_params
date = params[:appointment][:appointment_date] + ' ' + params[:appointment][:appointment_time]
appointment.appointment_date = date
check = DateTime.parse(appointment.appointment_date.to_s)
if check.wday == 6 || check.wday == 7
flash[:notice] = "Please chose Mon-Fri."
render :new
elsif check.hour > 9 || check.hour < 17
flash[:notice] = "Please chose the range between 09:00-17:00."
render :new
elsif
appointment.save
flash[:notice]= "Appointment is saved."
redirect_to appointment
else
render :new
end
end
flash[:notice] do not appear in any condition. render :new does not work and give the error as "First argument in form cannot contain nil or be empty"
Thanks in advance.
appointment = Appointment.new appointment_params
to@appointment = Appointment.new appointment_params
– Pavan