Wes Gamble
2006-Jul-13 22:38 UTC
[Rails] Does text_field go directly to attributes hash in AR object?
All, I''m having a hell of a time figuring out what is going on here. I''m trying to override one of my getters so that I can format it a certain way in my form. But I can''t seem to get text_field to call the appropriate method on my object. So here''s my getter: public def FAX fax = read_attribute(:FAX) puts fax sprintf("(%d)%d-%d", fax[0,3], fax[3,3], fax[6,4]) "crap" end Here''s a snippet of my template: <TD><%= @current_job.FAX %><%= text_field(:current_job, ''FAX'') %></TD> Fax: crap[text field with numbers in it - the value of the call to read_attribute] So my question is: QUESTION: Does text_field(:object, :method) call object.read_attribute(method) or object.method If it calls read_attribute, how can I get my facade method to be called from the template? Thanks, Wes -- Posted via http://www.ruby-forum.com/.
Wes Gamble
2006-Jul-13 22:46 UTC
[Rails] Re: Does text_field go directly to attributes hash in AR obj
This is because the helpers use value_before_type_cast to generate the tags, right? Man, that''s so irritating. Why is it like this? So, to verify, in general, any time that I want to override the display of one of my attributes, I have to: 1) Choose a new, unique name for another attribute (hereafter called the "display attribute") to represent the modified value 2) Create my getter to do what I want 3) Use attr_writer (or attr_accessor) to generate my setter 4) In the setter of the real attribute, access the value of my "display attribute" to actually get at what was submitted. Is that right? Thanks, Wes -- Posted via http://www.ruby-forum.com/.
Ezra Zygmuntowicz
2006-Jul-13 23:06 UTC
[Rails] Re: Does text_field go directly to attributes hash in AR obj
On Jul 13, 2006, at 3:46 PM, Wes Gamble wrote:> This is because the helpers use value_before_type_cast to generate the > tags, right? > > Man, that''s so irritating. Why is it like this? > > So, to verify, in general, any time that I want to override the > display > of one of my attributes, I have to: > > 1) Choose a new, unique name for another attribute (hereafter > called the > "display attribute") to represent the modified value > > 2) Create my getter to do what I want > > 3) Use attr_writer (or attr_accessor) to generate my setter > > 4) In the setter of the real attribute, access the value of my > "display > attribute" to actually get at what was submitted. > > Is that right? > > Thanks, > WesWes- You could also just use text_field_tag for this. Its a little annoying to not get the automated form fills with text_field but you can get around it with text_field_tag. -Ezra
Wes Gamble
2006-Jul-13 23:16 UTC
[Rails] Re: Re: Does text_field go directly to attributes hash in AR
Ezra Zygmuntowicz wrote:> On Jul 13, 2006, at 3:46 PM, Wes Gamble wrote: > >> called the >> Is that right? >> >> Thanks, >> Wes > > > Wes- > > You could also just use text_field_tag for this. Its a little > annoying to not get the automated form fills with text_field but you > can get around it with text_field_tag. > > -EzraThanks, I see that now. You can: 1) specify :value => @object.method on the text_field tag 2) use text_field_tag 3) Implement custom display getter/setter pair to front the real getter/setter pair. Do you understand the reasoning behind this - I think it''s so that if a user types something into a form that is invalid, then when the form refreshes (with their errors), they still see what they typed, not some modified version of it. Is that right? Is this a consequence of setting the attributes on the object _before_ validation occurs? If the value isn''t valid, then it probably shouldn''t be set on the object _at all_. I''m not saying we need JavaBeans/form beans here, I''m just sayin'' :). Am I on the right track? Thanks, WG -- Posted via http://www.ruby-forum.com/.
Wes Gamble
2006-Jul-13 23:21 UTC
[Rails] Re: Re: Does text_field go directly to attributes hash in AR
Wes Gamble wrote:> Ezra Zygmuntowicz wrote: >> On Jul 13, 2006, at 3:46 PM, Wes Gamble wrote: >> >>> called the >>> Is that right? >>> >>> Thanks, >>> Wes >> >> >> Wes- >> >> You could also just use text_field_tag for this. Its a little >> annoying to not get the automated form fills with text_field but you >> can get around it with text_field_tag. >> >> -Ezra > > Thanks, I see that now. > > You can: > > 1) specify :value => @object.method on the text_field tag > 2) use text_field_tag > 3) Implement custom display getter/setter pair to front the real > getter/setter pair. > > Do you understand the reasoning behind this - I think it''s so that if a > user types something into a form that is invalid, then when the form > refreshes (with their errors), they still see what they typed, not some > modified version of it. > > Is that right? Is this a consequence of setting the attributes on the > object _before_ validation occurs? If the value isn''t valid, then it > probably shouldn''t be set on the object _at all_. > > I''m not saying we need JavaBeans/form beans here, I''m just sayin'' :). > > Am I on the right track? > > Thanks, > WGYou know what - this totally sucks. I have 4 form fields that are actually two database fields that are split into two fields each. So I wrote my 4 getters to split apart the 2 fields so that I could display them, but because they aren''t _REAL_ attributes on the AR object, they get cleared out on the redisplay. The validation runs for them, but then they get cleared out. That is BS. The validation runs on the view facade methods, but the helpers go to the underlying data? WTF? What am I not getting? WG -- Posted via http://www.ruby-forum.com/.