from http://gabeodess.heroku.com/posts/14

I found myself wanting to set an attr_accessor to handle passing a date to my model in a form_for tag with the f.datetime_select helper. So this is what I had:

Model:

attr_accessor :my_time

View:

<%= f.datetime_select :my_time %>

Unfortunately when I submit my form I get this:

1 error(s) on assignment of multiparameter attributes

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_of as a replacement for attr_accessor. so…

Model:

  composed_of :my_time,
              :class_name => 'Time',
              :mapping => %w(Time to_s),
              :constructor => Proc.new{ |item| item },
              :converter => Proc.new{ |item| item }

I know almost nothing about the composed_of method 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, “Time”
  • mapping: 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_s”
  • constructor: Not really sure how this is supposed to work. Seems to be called when the @my_time is nil.
  • 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:

Time.zone.parse(stop_and_set.to_s(:number))
Tagged with:
 

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>