Daniel Lopes
2008-Dec-18 01:22 UTC
override a default attribute don''t behave like expected in form_for
Hello, I have a strange behavior and don''t know why. If I override attributes from AcitveRecord object like below: def end_date=(date) date = date.to_date unless date.blank? write_attribute(:end_date,date) end def end_date date = read_attribute(:end_date) I18n.localize(date) if date.present? end Everything will work fine but when I try call text_field in a form_for block I get the database value and ignore end_date getter. <%= f.text_field :end_date, :class=>"date", :size=>"10" %> The text_field will be filled with 2008-12-10 (mysql date) instead of 10/12/2008 ( I18n localized) Anyone know why we get this behavior? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Daniel Lopes
2008-Dec-18 01:30 UTC
Re: override a default attribute don''t behave like expected in form_for
Looking inside rails code I found text_field use InstanceTag and this call []before_type_cast and will return my value before turned in ruby object. What is the benefit os this behavior? I can solve my problem forcing value on text_field but I think it is ugly. On Dec 17, 11:22 pm, "Daniel Lopes" <danielvlo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello, I have a strange behavior and don''t know why. > > If I override attributes from AcitveRecord object like below: > > def end_date=(date) > date = date.to_date unless date.blank? > write_attribute(:end_date,date) > end > > def end_date > date = read_attribute(:end_date) > I18n.localize(date) if date.present? > end > > Everything will work fine but when I try call text_field in a form_for > block I get the database value and ignore end_date getter. > > <%= f.text_field :end_date, :class=>"date", :size=>"10" %> > > The text_field will be filled with 2008-12-10 (mysql date) instead of > 10/12/2008 ( I18n localized) > > Anyone know why we get this behavior?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Daniel Lopes
2008-Dec-18 01:49 UTC
Re: override a default attribute don''t behave like expected in form_for
I fix my problem with alias between attribute and attribute_before_type_cast ... I think active record do this because other kind of columns like decimals or numbers ( i''m not sure ) but it''s not beautyfull when we need make alias only for make text_field behave like expected. On Dec 17, 11:30 pm, Daniel Lopes <danielvlo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Looking inside rails code I found text_field use InstanceTag and this > call []before_type_cast and will return my value before turned in ruby > object. > What is the benefit os this behavior? > > I can solve my problem forcing value on text_field but I think it is > ugly. > > On Dec 17, 11:22 pm, "Daniel Lopes" <danielvlo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hello, I have a strange behavior and don''t know why. > > > If I override attributes from AcitveRecord object like below: > > > def end_date=(date) > > date = date.to_date unless date.blank? > > write_attribute(:end_date,date) > > end > > > def end_date > > date = read_attribute(:end_date) > > I18n.localize(date) if date.present? > > end > > > Everything will work fine but when I try call text_field in a form_for > > block I get the database value and ignore end_date getter. > > > <%= f.text_field :end_date, :class=>"date", :size=>"10" %> > > > The text_field will be filled with 2008-12-10 (mysql date) instead of > > 10/12/2008 ( I18n localized) > > > Anyone know why we get this behavior?--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---