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

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

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?


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

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!