0
votes

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.

1
You have to change this line appointment = Appointment.new appointment_params to @appointment = Appointment.new appointment_paramsPavan
Thanks, Pavan. Yes. It works after changing to @appointment = Appointment.new appointment_params. Also, my friend suggests me to add <%= flash[:notice] %> for in _form.html.erb to appear the flash notice.sugi1119

1 Answers

0
votes

Try flash.now.notice = "Please chose the range between 09:00-17:00."