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:

<%= @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

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

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

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs