Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #6908 (closed defect: invalid)

Opened 2 years ago

Last modified 2 years ago

[PATCH] Form Helper's text field tag ignores user redefined methods

Reported by: ThiagoArrais Assigned to: core
Priority: normal Milestone: 1.2
Component: ActionPack Version: edge
Severity: normal Keywords:
Cc:

Description

When a model attribute methods have been manually overwritten by the programmer, FormHelper's text_field method ignores it and uses 'method_before_type_cast' instead. This is inconsistent with the docs at http://api.rubyonrails.org/classes/ActionView/Helpers/FormHelper.html that state that

<%= text_field "person", "name", "size" => 20 %>

is equivalent to

<input type="text" id="person_name" name="person[name]"
      size="20" value="<%= @person.name %>" />

But it is rather equivalent to

<input type="text" id="person_name" name="person[name]"
      size="20" value="<%= @person.name_before_type_cast %>" />

My patch fixes that and all actionpack tests still pass after the change. But this patch may still be unsafe. I assume someone chose to use value_before_type_cast with some reason.

Attachments

text_field_helper_ignores_redefined_method.diff (1.3 kB) - added by ThiagoArrais on 01/01/07 15:35:51.

Change History

01/01/07 15:35:51 changed by ThiagoArrais

  • attachment text_field_helper_ignores_redefined_method.diff added.

01/01/07 15:59:20 changed by ThiagoArrais

The revision that introduced before_type_cast was http://dev.rubyonrails.org/changeset/215

(follow-up: ↓ 3 ) 01/01/07 20:07:43 changed by nzkoz

  • status changed from new to closed.
  • resolution set to invalid.

The use of .value_before_typecast is for numeric attributes, and is intentional.

It ensures that rails remembers the right values if you have 'validates_numericality_of' and the user types in non-numeric characters. If you'd like to contribute some documentation to that effect, please feel free to open another ticket.

(in reply to: ↑ 2 ) 01/02/07 11:12:37 changed by ThiagoArrais

I would rather write the tests to show the need for *_before_type_cast. The problem is I didn't really understand the issue with numeric attributes and validates_numericality_of and how does that connect to FormHelper.

Would you mind clarifying that point?