Forum: Ruby on Rails date_select and "31st February"

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Cb880dd8c8c209924ebb57edbb4fcd06?d=identicon&s=25 Jim Burgess (jim_in_germany)
on 2009-04-03 14:38
Hi,

I'm writing a form where the user should enter their date of birth.

Here's the code:

<% form_for :applicant, :url=> {:action => "index"} do |f| %>
...
<%= f.label :dob, "Date of Birth" %>
<%= f.date_select :dob, :include_blank => true %>
..
<p><%= submit_tag "Submit" %></p>
<% end %>

When I open the form in my browser everything is fine.
However when I select "February" as the month, "31" as the day, don't
select a year, and then press "Submit" I get the following error
message:

1 error(s) on assignment of multiparameter attributes

Can anyone tell me why this is happening and how to avoid this?

Thanks in advance.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-04-03 14:54
(Received via mailing list)
On 3 Apr 2009, at 13:38, Jim Burgess wrote:

> <%= f.date_select :dob, :include_blank => true %>
>
> Can anyone tell me why this is happening and how to avoid this?

because rails is just faithfully passing this down and trying to
create a date object for 31st february.

update_attributes, Applicant.new etc. will raise
ActiveRecord::MultiparameterAssignmentErrors when this happens.

Fred
Cb880dd8c8c209924ebb57edbb4fcd06?d=identicon&s=25 Jim Burgess (jim_in_germany)
on 2009-04-03 15:02
Thanks for the quick reply.
It is important for my form that the date_select helper is not
initialized with a value (ie. :include_blank => true).
So, how could I stop the above error from happening?
Could you give me a hint as to which direction to be thinking in?
I'm still relatively new to Rails, so I would be grateful if any
explination isn't too complicated.
Thanks once again.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-04-03 15:22
(Received via mailing list)
On 3 Apr 2009, at 14:02, Jim Burgess wrote:

>
> Thanks for the quick reply.
> It is important for my form that the date_select helper is not
> initialized with a value (ie. :include_blank => true).
> So, how could I stop the above error from happening?

You can't easily stop the user entering dates like that with rails
built in date helpers, so you need to rescue those exceptions and
massage them into a friendly message.

Fred
Cb880dd8c8c209924ebb57edbb4fcd06?d=identicon&s=25 Jim Burgess (jim_in_germany)
on 2009-04-03 17:29
> You can't easily stop the user entering dates like that with rails
> built in date helpers, so you need to rescue those exceptions and
> massage them into a friendly message.

Hi Fred,
I'm quite surprised, as I would have thought that this makes the entire
date_select helper open to misuse.
Nonetheless, two further questions:

How does one rescue exceptons in rails? I just need a point to start.

Would it be a feasible option to use three text fields in the form that
are unrelated to the model. Then in the controller string them together
and make a date object? (that would be more or less how I would do it in
PHP).
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2009-04-03 17:52
(Received via mailing list)
On Apr 3, 4:29 pm, Jim Burgess <rails-mailing-l...@andreas-s.net>
wrote:
> > You can't easily stop the user entering dates like that with rails
> > built in date helpers, so you need to rescue those exceptions and
> > massage them into a friendly message.
>
> Hi Fred,
> I'm quite surprised, as I would have thought that this makes the entire
> date_select helper open to misuse.

date_select is simple & stupid. Seems to me like the worst case is
someone enters an invalid date and the create fails.

> Nonetheless, two further questions:
>
> How does one rescue exceptons in rails? I just need a point to start.
>
http://www.rubycentral.com/pickaxe/tut_exceptions.html

> Would it be a feasible option to use three text fields in the form that
> are unrelated to the model. Then in the controller string them together
> and make a date object? (that would be more or less how I would do it in
> PHP).

You could. You'd still have to rescue the exception that is raised by
Date::civil when you pass it 2009,2,29 (or do all the bounds checking
yourself)

Fred
Cb880dd8c8c209924ebb57edbb4fcd06?d=identicon&s=25 Jim Burgess (jim_in_germany)
on 2009-04-04 14:21
Just thought I'd let you know how things turned out.

I have discarded the idea of using date_select as it is full of
inconsistencies which in the worst case can cause the application to
crash.
Instead I created three seperate text fields (DD-MM-YYYY)and using
virtual attributes in the model mapped them to the "dob" field in the
db.
Now everything works like it should do (at last).

I am surprised that this has presented me with such a problem as
entering one's dob into a form is a very common occurence.

Nonetheless thanks for your help Fred. You definitely pointed me in the
right direction.
This topic is locked and can not be replied to.