# Does text\_field go directly to attributes hash in AR object?

**URL:** <https://www.ruby-forum.com/t/does-text-field-go-directly-to-attributes-hash-in-ar-object/98347>\
**Category:** Rails\
**Created:** [July 14, 2006, 12:37am UTC](https://www.ruby-forum.com/t/does-text-field-go-directly-to-attributes-hash-in-ar-object/98347 "2006-07-14T00:37:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 14, 2006, 12:37am UTC](https://www.ruby-forum.com/t/does-text-field-go-directly-to-attributes-hash-in-ar-object/98347/1 "2006-07-14T00:37:57Z")

</div>

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:

\<%= @current\_job.FAX %\>\<%= text\_field(:current\_job, 'FAX') %\>

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

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 14, 2006, 12:46am UTC](https://www.ruby-forum.com/t/does-text-field-go-directly-to-attributes-hash-in-ar-object/98347/2 "2006-07-14T00:46:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 14, 2006, 1:07am UTC](https://www.ruby-forum.com/t/does-text-field-go-directly-to-attributes-hash-in-ar-object/98347/3 "2006-07-14T01:07:41Z")

</div>

On Jul 13, 2006, at 3:46 PM, Wes G. 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

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 14, 2006, 1:16am UTC](https://www.ruby-forum.com/t/does-text-field-go-directly-to-attributes-hash-in-ar-object/98347/4 "2006-07-14T01:16:34Z")

</div>

Ezra Z. wrote:

> On Jul 13, 2006, at 3:46 PM, Wes G. 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,  
WG

---

<div class="post-metadata">

**Author:** ![weyus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/7ab992/32.png) [@weyus](https://www.ruby-forum.com/u/weyus)\
**Post date:** [July 14, 2006, 1:20am UTC](https://www.ruby-forum.com/t/does-text-field-go-directly-to-attributes-hash-in-ar-object/98347/5 "2006-07-14T01:20:46Z")

</div>

Wes G. wrote:

> Ezra Z. wrote:
> 
> > On Jul 13, 2006, at 3:46 PM, Wes G. 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,  
> WG

You 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
