Forum: Ruby on Rails Form helpers and overloaded methods - help!

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
60e38de043848f82392062088f191213?d=identicon&s=25 Hogan, Brian P. (Guest)
on 2006-03-01 22:00
(Received via mailing list)
Can someone explain why form helpers appear to bypass any model methods
I override for fields that are bound to database fields? It would be
great if someone could tell me how to force the form field, etc to call
the method instead of looking at the database / attributes collection.

Let's say I have a column called 'price' in my database table "books"

Let's say I want price to always come back as a special format when I
query my model.... So in my model, I do

   def price
      number_with_precision(self.attributes[:price])
   end

In a view, @book.price comes back formatted correctly as expected.

However,  <%=text_field "book", "price" %> bypasses my method and gets
the unformatted price.

It just doesn't seem consistent. I'm currenly playing around with a hack
to form_helper et all, but it's kinda complicated and so I was looking
for an explanation or an easy workaround. (I know I can set the
precision in my db to accomplish this issue but this is just an
example.)


Thanks much to anyone willing to help.




Brian Hogan
Web Development
Learning & Technology Services
Schofield 3-B
University of Wisconsin-Eau Claire
715 836 3585
hoganbp@uwec.edu



Brian Hogan
Web Development
Learning & Technology Services
Schofield 3-B
University of Wisconsin-Eau Claire
715 836 3585
hoganbp@uwec.edu
73abd1d78de7a113a729484d8e9ceb71?d=identicon&s=25 Alex Night (alex_n)
on 2011-11-30 00:37
i`m in the same situation and did the following in view:
<%= text_field_tag('book[price]', @book.price) %>
This topic is locked and can not be replied to.