Date from user input

Hello,

I am trying to create a Date instance from user input, but I keep

getting the ArgumentError invalid date. Here’s the simplified version:

View:

<%= form_tag “/” do %>
<%= number_field_tag(:month) %>
<%= number_field_tag(:day) %>
<%= number_field_tag(:year) %>
<%= submit_tag “Date” %>
<%= end %>

Controller:

Date.new(params[:year].to_i, params[:month].to_i, params[:day].to_i)

I find this odd because I haven’t even inputted any variables in the
view yet the date is invalid?

Thanks

On Monday, April 25, 2011 12:00:43 PM UTC-4, Ruby-Forum.com User wrote:

I find this odd because I haven’t even inputted any variables in the
view yet the date is invalid?

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

could not create date from parameters

end

Tim S. wrote in post #994906:

On Monday, April 25, 2011 12:00:43 PM UTC-4, Ruby-Forum.com User wrote:

I find this odd because I haven’t even inputted any variables in the
view yet the date is invalid?

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?

Thanks for the reply, makes sense.

How would I write an if statement like that for a form_tag form?

Tim S. wrote in post #994920:

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[:day].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.

I must have made some mistake because I checked

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?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs