This project is archived and is in readonly mode.
text_field() does not use overridden model accessors
-
Dennis Theisen
- Tag changed from text_field to form_for, text_field
-
Rohit Arondekar
- Assigned user set to Rohit Arondekar
Dennis, can you confirm if this issue still exists on 2.3.8 and Rails master?
-
Rohit Arondekar
- Tag changed from form_for, text_field to accessor, form_for, text_field
- State changed from new to open
- Assigned user changed from Rohit Arondekar to Jeremy Kemper
I can confirm this issue exists on Rails master.
The model has a title field.
class Post < ActiveRecord::Base def title "blah blah" end endThe edit form that should show "blah blah" in the text field.
<%= form_for(@post) do |f| %> <div class="field"> <%= f.label :title %><br /> <%= f.text_field :title %> </div> <div class="actions"> <%= f.submit %> </div> <% end %>It fetches the text directly from the DB bypassing the title method.
-
Rohit Arondekar
Can somebody please confirm on the 2-3-stable branch or 2.3.8?
-
Sam Soffes
- Tag changed from accessor, form_for, text_field to form_for, text_field
- Assigned user changed from Jeremy Kemper to Rohit Arondekar
I just tested this in Rails 3.0.0.beta4 and experienced the same problem.
I created a new app, ran
rails g migration post title:string, and made post look like this:class Post < ActiveRecord::Base def title "blah blah" end endThe textfield in both the new and edit, didn't show "blah blah" as the title, but an empty string or what was in the database in edit action. In the console, it behaved as expected.
-
Rohit Arondekar
- Assigned user changed from Rohit Arondekar to Jeremy Kemper
-
Santiago Pastorino
- No changes were found…
-
Santiago Pastorino
- Milestone cleared.
- Assigned user changed from Jeremy Kemper to Santiago Pastorino
-
Santiago Pastorino
- Milestone cleared.
-
Miles Egan
- Importance changed from to Low
This seems to be a result of this definition in lib/action_view/form_helper.rb:
def value_before_type_cast(object, method_name) unless object.nil? object.respond_to?(method_name + "_before_type_cast") ? object.send(method_name + "_before_type_cast") : object.send(method_name) end endWhich means that we only call the native method if we can't find a corresponding before_type_cast version. This seems backwards. If we define an accessor that should take priority, right? Reversing the sense of this method to call method_name first and then fall back to before_type_cast doesn't seem to break any tests.
-
Repository
- State changed from open to committed
(from [fb0bd8c1092db51888ec4bb72af6c595e13c31fa]) Makes form_helper use overriden model accessors
[#3374 text_field() does not use overridden model accessors state:committed] http://github.com/rails/rails/commit/fb0bd8c1092db51888ec4bb72af6c5...
-
Repository
(from [8141f0894ecf869ee11f077e5b49e06669790632]) Makes form_helper use overriden model accessors backport
[#3374 text_field() does not use overridden model accessors] http://github.com/rails/rails/commit/8141f0894ecf869ee11f077e5b49e0...
-
Chris Wise
With the commit to RC2, there are some side-effects to this with Time fields and MySQL (2.8.1 gem). It used to be that when text_field was used with a Time field (not Date), that the value would be rendered as "13:00:00" (as an example). Now it renders as "Sat Jan 01 13:00:00 UTC 2000".
Is this more of a problem with the MySQL gem, or in ActiveRecord (I am sorry, I can't follow the code well enough to know)? Also in the committed fix (http://github.com/rails/rails/commit/fb0bd8c1092db51888ec4bb72af6c5..., why would the object.send(method_name + "_before_type_cast") be allowed w/o checking if the object respond to it (Again, I am weak at working through the code and trying to figure out if that method is required to be there for each attribute - maybe it is).
def value_before_type_cast(object, method_name) unless object.nil? object.respond_to?(method_name) ? object.send(method_name) : object.send(method_name + "_before_type_cast") end end -
Chris Wise
I rescind my comment. I was informed I was using mysql gem (2.8.1) and should've been using mysql2 gem instead. There is some funny behaviour from the mysql gem where I was getting an expected rendering as a time but that's apparently not what the behaviour is expected to be.
-
Oliver Eilhard
The commit 8141f0894ecf869ee11f077e5b49e06669790632 seems to break a User in one of my applications that has a write-only password field.
The User model comes with a password attribute which is write-only and hashed before written to the DB, so there's only the following code in the User model:
def password=(cleartext_password) @password = cleartext_password endIn the form I'm using the form helper like this:
f.password_field :passwordNow when rendering the form, the form_helper is complaining about a missing
password_before_type_castmethod which it didn't before RC2. -
Repository
(from [f95ba5c262bfd713f0a2fc656f8e645d3eea60f2]) Make InstanceTagMethods#value_before_type_cast raise if the model don't respond to attr_before_type_cast or attr method
[#3374 text_field() does not use overridden model accessors] [#5471 FormHelper problem with write-only fields state:committed] http://github.com/rails/rails/commit/f95ba5c262bfd713f0a2fc656f8e64...
-
Repository
(from [3ba8e3100548f10fce0c9784981a4589531476dd]) Make InstanceTagMethods#value_before_type_cast raise if the model don't respond to attr_before_type_cast or attr method
[#3374 text_field() does not use overridden model accessors] [#5471 FormHelper problem with write-only fields state:committed] http://github.com/rails/rails/commit/3ba8e3100548f10fce0c9784981a45...
-
Noah
This commit makes the error message a bit obscure. If, for example, you use a custom Person object and present the field "name", but your Person object doesn't define name I would expect to see "undefined method
name' for #<Person:0xf661188>" and not "undefined methodname_before_type_cast' for #<Person:0xf661188>" -
Santiago Pastorino
Right send a patch file ;)
-
Tom Stuart
There's some complaining on the 2.3 backport, specifically about the fact that this breaks the behaviour whereby the user's original inputs (vs their typecasted equivalents) are kept on the form after a validation failure.
No longer a problem for 2.3.x since it's been reverted on 2-3-stable, but out of interest, why's this not a problem for Rails 3 too?
-
Santiago Pastorino
Tom we discussed about this and i'm going to revert it from Rails 3 too.
-
Tom Stuart
Great, Santiago. Thanks for clarifying!
-
Tom Stuart
Resolved by this commit, I believe. Thanks.