# How does a template know to use attributes before type cast after a validation error

**URL:** <https://www.ruby-forum.com/t/how-does-a-template-know-to-use-attributes-before-type-cast-after-a-validation-error/143900>\
**Category:** Rails\
**Created:** [July 23, 2008, 6:03pm UTC](https://www.ruby-forum.com/t/how-does-a-template-know-to-use-attributes-before-type-cast-after-a-validation-error/143900 "2008-07-23T18:03:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tonypm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@tonypm](https://www.ruby-forum.com/u/tonypm)\
**Post date:** [July 23, 2008, 6:03pm UTC](https://www.ruby-forum.com/t/how-does-a-template-know-to-use-attributes-before-type-cast-after-a-validation-error/143900/1 "2008-07-23T18:03:18Z")

</div>

I wonder if anyone could help me with this.

In normal circumstances, re-rendering a form fields with validation  
errors, show original entered data and not type\_cast data from the  
object.

However, I have a situation where I am submitting a number of values  
to do a bulk update. My model converts the returned values and does  
the necessary processing, building the data structure from the request  
params hash.

However, when I use this to re-display the form using render :action,  
the values I get are the ones after type\_cast. ie. ‘x’ entered for  
numeric becomes 0

I know from my unit tests that the model has built my object with the  
correct associations, and the objects with validation errors are still  
shown as new\_record and the attribute\_before\_validation is there as  
entered by the user.

So I am wondering what Rails uses to determine how to redisplay the  
fields (I had assumed that it checked for new\_record and used  
attribute\_before\_validation). But this does not seem to be the case

I can only think that it is done from the params hash. Now in my  
case, the input fields do not follow the conventional object name  
derivation, but the naming would be consistent with the params hash.

The only reason I can come up with at the moment for the post  
validation values being shown is that Rails does some sort of cross  
reference between the input field name values, the params hash and the  
object values. Which I have made not consistent

Any ideas?

Thanks  
Tonypm

---

<div class="post-metadata">

**Author:** ![tonypm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/a587f6/32.png) [@tonypm](https://www.ruby-forum.com/u/tonypm)\
**Post date:** [July 23, 2008, 6:45pm UTC](https://www.ruby-forum.com/t/how-does-a-template-know-to-use-attributes-before-type-cast-after-a-validation-error/143900/2 "2008-07-23T18:45:36Z")

</div>

OK - I have my answer.

Rails helpers can only get at the object’s  
attribute\_before\_validation, if the helper has the object passed to  
it.

In my case, I was using text\_field\_tag which of course gets the value  
and not the object. It was staring me in the face really.

So in my case, I can use the text\_field with model, attribute symbols,  
and set the name to be whatever I want. And that works.

Silly me!

Tonypm
