Re: Why is FormHelper using value_before_type_cast ?

Michael Koziarski
Mon, 06 Mar 2006 03:35:07 -0000

On 3/6/06, Francois Beausoleil <francois.beausoleil@gmail.com>
wrote:
> Hi !
>
> Just wondering here, because if I define reader methods, then these
> aren''t used in the forms.
>
> For example, I have this:
> class Customer
>   def tax_rate
>     (read_attribute(:tax_rate) * 100.0).to_i
>   end
>
>   def tax_rate=(rate)
>     write_attribute(:tax_rate, rate / 100.0)
>   end
> end
>
> <%= text_field :customer, :tax_rate %>
>
> For 7.5, the value shown on screen is 750.  Of course, I could do:
> <%= text_field :customer, :tax_rate, :value => @customer.tax_rate
%>
>
> but that gets pretty boring real quick...
>
> So, what is the rationale behind this decision ?  Are the date/time
> helpers the ones that need the value_before_type_cast ?

Otherwise the user''s data will be ''randomly'' changed
by the system.   i.e.

irb(main):001:0> "asdifjasdliofj".to_i
=> 0
irb(main):002:0> "123,40".to_i
=> 123

So if it didn''t use _before_typecast, and the user typed either of
those values, then when the page was re-rendered,  the values won''t
have been remembered correctly.

The previous behaviour was worse and led to many bug reports about 0
appearing where it wasn''t expected.

--
Cheers

Koz