venerdì 28 ottobre 2011

Using virtual attributes for multi-parameter form helpers in Rails

In a Rails application I am working on, I needed to setup a form with a field with a non-standard behaviour. The field represents a date object, so the FormHelper date_select helper looked great; however, the date to display was not the actual date to be set on the database, but the day before. Changing all the data on the DB was a bit risky, so I had to stick with this requirement.
I decided to use a virtual attribute to do this, as it seemed the most elegant solution, so I wrote this in my model:

def expire_date_minus_one_day
self.expire_date - 1.day
end
def expire_date_minus_one_day= date
self.expire_date = date + 1.day
end


And this in my controller:

<p>
<%= f.label :expire_date %>
<%= f.date_select :expire_date_minus_one_day %>
</p>


However, trying to send this data to the controller resulted in a "1 error(s) on assignment of multiparameter attributes" error.

The solution I found after some search was this one:

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


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

Nessun commento:

Posta un commento