I have the following code in my form.
<%= f.datetime_select(:date_time, :prompt => {:day => 'Day', :month => 'Month', :year => 'Year'}, :start_year => Date.today.year, :end_year => Date.today.year + 2, :minute_step => 15, :include_blank => false) %> if either one is blank.
When one of the fields is left blank, I get:
1 error(s) on assignment of multiparameter attributes
The params that are being passed are:
{"utf8"=>"✓",
"authenticity_token"=>"kQpfsj5RxnDtxkvBdwPEFnX1fY6euKnMQeDRAkvJvIE=",
"event"=>{"description"=>"",
"venue"=>"",
"street"=>"",
"city"=>"",
"country_id"=>"",
"date_time(1i)"=>"",
"date_time(2i)"=>"",
"date_time(3i)"=>"",
"date_time(4i)"=>"00",
"date_time(5i)"=>"00",
"ticket_url"=>""},
"x"=>"94",
"y"=>"12"}
Anyone know why this is occurring?
There seems to be a “dirty” fix for this at this link, but perhaps there is a better solution in Rails 3?
Christian. This is a bug in Rails that checks the database to infer the type needed for the multiparameter attributes. My guess is that your “date_time” attribute is not associated with a time column in your database.
I recently tackled this problem where I wanted a non-database attribute to accepted multiparameter attributes, this was the best solution I could come up with:
I found myself wanting to set an
attr_accessorto handle passing a date to my model in aform_fortag with thef.datetime_selecthelper. So this is what I had:Model:
View:
Unfortunately when I submit my form I get this:
Well it turns out that this is actually a Rails bug a ticket for which has been submitted. In the meantime how do we make this work? The only solution I could find that was remotely attractive was to make use of
composed_ofas a replacement forattr_accessor. so…Model:
I know almost nothing about the
composed_ofmethod so you should probably do your own reading on it, but what I do know is that it creates both a reader and writer for the given instance variable, and more importantly, the setter accepts multiparameter attributes. How I chose the options:class_name: the name of our expected class. In this case,
Timemapping: the first argument is the class and the second argument seems to work with any method that an instance of the class responds to. I chose
to_sconstructor: Not really sure how this is supposed to work. Seems to be called when
@my_timeisnil.converter: Not really sure how this is supposed to work. Seems to be called when from my_time=, but doesn’t seem to be applied with mass assignment.
One problem I ran into with this solution was that times were getting set in UTC instead of the environment’s time zone. So unfortunately we cannot use my_time directly, but instead need to convert it to the proper time zone: