Actually that’s probably exactly why you have an invalid date. If you haven’t input any variables yet, params hash will be empty. This means the code you posted basically evaluates to the following:
Date.new(nil.to_i, nil.to_i, nil.to_i)
Which returns your ArgumentError.
Can you wrap the Date.new call in an if statement so it only gets executed once the form has been submitted?
The easiest way is probably just “if request.post?”
Be careful though - it could be a post request without the parameters you are looking for. Might also be helpful to check if the params hash contains the keys you plan on using.
Another option is to just try it, then ask for forgiveness:
begin
Date.new(params[:year].to_i, params[:month].to_i, params[:day].to_i)
rescue ArgumentError
The easiest way is probably just "if request.post?"
Be careful though - it could be a post request without the parameters
you
are looking for. Might also be helpful to check if the params hash
contains
the keys you plan on using.
Another option is to just try it, then ask for forgiveness:
begin
Date.new(params[:year].to_i, params[:month].to_i, params[:day].to_i)
rescue ArgumentError
# could not create date from parameters
end
if request.post?
Date.new(params[:year].to_i, params[:month].to_i, params[:day].to_i)
end
Still throws the same invalid date error.
I also tried:
unless params[:eff_day].to_i.nil?
Date.new(params[:year].to_i, params[:month].to_i, params[:day].to_i)
end
And that at least does not throw an error, but it doesn't seem to work
either.
if request.post?
Date.new(params[:year].to_i, params[:month].to_i, params[:day].to_i)
end
again and it does not call an error either. However anytime I input any
numbers into the fields, i get the same invalid date error. Is there
something wrong with using params[:year].to_i?