I decided to use a virtual attribute to do this, as it seemed the most elegant solution, so I wrote this in my model:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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