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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060301/1963ef80/attachment-0001.html
Well, here''s the code that makes the form tags look at the database directly. Is there a reason why I shouldn''t comment out these lines below? Basically, I''m asking the core team why they decided to look at the database first. module ActionView module Helpers class InstanceTag def value_before_type_cast unless object.nil? # object.respond_to?(@method_name + "_before_type_cast") ? # object.send(@method_name + "_before_type_cast") : object.send(@method_name) end end end end -----Original Message----- From: rails-bounces@lists.rubyonrails.org [mailto:rails-bounces@lists.rubyonrails.org] On Behalf Of Hogan, Brian P. Sent: Wednesday, March 01, 2006 2:59 PM To: rails@lists.rubyonrails.org Subject: [Rails] Form helpers and overloaded methods - help! 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060301/4252ce5f/attachment.html
Brian, I have the same problem. I don''t have an answer for you but I think the more appropriate place to modify the code is at (line after !!! comment): def to_input_field_tag(field_type, options = {}) options = options.stringify_keys options["size"] ||= options["maxlength"] || DEFAULT_FIELD_OPTIONS["size"] options = DEFAULT_FIELD_OPTIONS.merge(options) if field_type == "hidden" options.delete("size") end options["type"] = field_type # !!! options["value"] ||= value_before_type_cast unless field_type == "file" options["value"] ||= value unless field_type == "file" add_default_name_and_id(options) tag("input", options) end I have a suspicion that value_before_type_cast was used for things that don''t degrade well to a string after type casting. What is interesting is it appears that the typical form processing is not symmetrical, when I instantiate an object using the form parameters when processing the submit, it does go through the overridden methods. For now, I have made the above change in my development environment and am continuing to test with it. Eric
i`m in the same situation and did the following in view: <%= text_field_tag(''book[price]'', @book.price) %> -- Posted via http://www.ruby-forum.com/. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.