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 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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? -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Tim Shaffer 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? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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 -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Tim Shaffer 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 > endif 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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
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? -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.