# Values in form fields lost after validation

**URL:** <https://www.ruby-forum.com/t/values-in-form-fields-lost-after-validation/71117>\
**Category:** Rails\
**Created:** [October 5, 2006, 2:07pm UTC](https://www.ruby-forum.com/t/values-in-form-fields-lost-after-validation/71117 "2006-10-05T14:07:01Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![yussibaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e79b87/32.png) [@yussibaer](https://www.ruby-forum.com/u/yussibaer)\
**Post date:** [October 5, 2006, 2:07pm UTC](https://www.ruby-forum.com/t/values-in-form-fields-lost-after-validation/71117/1 "2006-10-05T14:07:01Z")

</div>

Hi all,

After validation of a form field rails keeps the entered data in the  
form. The strange thing is that the entered data are kept in some fields  
but not in every field. Although the

-tags are almost the same. In  
the following form field the entered data is kept after the validation Name: \<%=text\_field("customer", "first\_name")%\>

In the following the value is lost after validation

Strasse: \<%=text\_field("contact\_detail", "address1")%\>

Did anyone struggle over the same issue?

---

<div class="post-metadata">

**Author:** ![yussibaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e79b87/32.png) [@yussibaer](https://www.ruby-forum.com/u/yussibaer)\
**Post date:** [October 5, 2006, 6:03pm UTC](https://www.ruby-forum.com/t/values-in-form-fields-lost-after-validation/71117/2 "2006-10-05T18:03:08Z")

</div>

On Oct 5, 1:07 pm, Yu Co [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> After validation of a form field rails keeps the entered data in the  
> form. The strange thing is that the entered data are kept in some fields  
> but not in every field. Although the -tags are almost the same. In  
> the following form field the entered data is kept after the validation
> 
> Name: \<%=text\_field("customer", "first\_name")%\>
> 
> In the following the value is lost after validation
> 
> Strasse: \<%=text\_field("contact\_detail", "address1")%\>

Can you post your controller code? It looks like the @contact\_detail  
object isn’t being populated. It’s probably something to do with the  
order that you’re assigning and validating things.

Chris

---

<div class="post-metadata">

**Author:** ![yussibaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e79b87/32.png) [@yussibaer](https://www.ruby-forum.com/u/yussibaer)\
**Post date:** [October 6, 2006, 1:32am UTC](https://www.ruby-forum.com/t/values-in-form-fields-lost-after-validation/71117/3 "2006-10-06T01:32:18Z")

</div>

Chris M. wrote:

> On Oct 5, 1:07 pm, Yu Co [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > After validation of a form field rails keeps the entered data in the  
> > form. The strange thing is that the entered data are kept in some fields  
> > but not in every field. Although the -tags are almost the same. In  
> > the following form field the entered data is kept after the validation
> > 
> > Name: \<%=text\_field("customer", "first\_name")%\>
> > 
> > In the following the value is lost after validation
> > 
> > Strasse: \<%=text\_field("contact\_detail", "address1")%\>
> 
> Can you post your controller code? It looks like the @contact\_detail  
> object isn’t being populated. It’s probably something to do with the  
> order that you’re assigning and validating things.
> 
> Chris

Hi Chris,

the relevant code piece of the corresponding controller looks like this:  
â??  
.  
.  
.

def create  
@contact\_detail = ContactDetail.new(params[:contact\_detail])  
@customer = Customer.new(params[:customer])  
@customer.contact\_detail = @contact\_detail  
.  
.  
.  
if @customer.save  
.  
.  
session[‘user’] = @customer  
session[:customer] = nil  
.  
.

```
 else

 begin
   session[:errors] = @errors
 end

```

end  
â?¦  
â?

---

<div class="post-metadata">

**Author:** ![yussibaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e79b87/32.png) [@yussibaer](https://www.ruby-forum.com/u/yussibaer)\
**Post date:** [October 6, 2006, 12:29pm UTC](https://www.ruby-forum.com/t/values-in-form-fields-lost-after-validation/71117/4 "2006-10-06T12:29:03Z")

</div>

Yu Co wrote:

> ```
> else
> 
> begin
> session[:errors] = @errors
> end
> 
> ```
> 
> end

I can’t see how you’re redisplaying the form. Are you doing a  
render(:action =\> :new)? Are you redirecting back to the ‘new’ action  
using redirect\_to(:action =\> :new)? Or do you have a separate  
create.rhtml template?

Chris

---

<div class="post-metadata">

**Author:** ![yussibaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e79b87/32.png) [@yussibaer](https://www.ruby-forum.com/u/yussibaer)\
**Post date:** [October 9, 2006, 4:08am UTC](https://www.ruby-forum.com/t/values-in-form-fields-lost-after-validation/71117/5 "2006-10-09T04:08:57Z")

</div>

Chris M. wrote:

> Yu Co wrote:
> 
> > ```
> > else
> > 
> > begin
> > session[:errors] = @errors
> > end
> > 
> > ```
> > 
> > end
> 
> I can’t see how you’re redisplaying the form. Are you doing a  
> render(:action =\> :new)? Are you redirecting back to the ‘new’ action  
> using redirect\_to(:action =\> :new)? Or do you have a separate  
> create.rhtml template?
> 
> Chris

aaa sorry for that. The relevant code piece looks like that:

```
    session[:errors] = @errors
    session[:customer] = @customer
    redirect_to :controller => 'auth', :action => 'register'

```

and register.html:  
\<%= start\_form\_tag :controller =\> ‘customer’, :action =\> ‘create’ %\>  
\<%= render :partial =\> ‘customer/registrationform’ %\>

 \<%= submit\_tag "Register and Order" %\> \<%= end\_form\_tag %\>

---

<div class="post-metadata">

**Author:** ![yussibaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e79b87/32.png) [@yussibaer](https://www.ruby-forum.com/u/yussibaer)\
**Post date:** [October 9, 2006, 5:59am UTC](https://www.ruby-forum.com/t/values-in-form-fields-lost-after-validation/71117/6 "2006-10-09T05:59:41Z")

</div>

and the controller auth looks like this:

def register  
@customer = session[:customer] == nil ? Customer.new :  
session[:customer]  
@errors = session[:errors]  
end

I checked the values within ‘register’ action. And indeed the entered  
data in the form is available under @customer.contact\_detail. But after  
submitting the form the contact\_detail dates are not shown but the  
customer dates.

---

<div class="post-metadata">

**Author:** ![yussibaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e79b87/32.png) [@yussibaer](https://www.ruby-forum.com/u/yussibaer)\
**Post date:** [October 10, 2006, 7:09am UTC](https://www.ruby-forum.com/t/values-in-form-fields-lost-after-validation/71117/7 "2006-10-10T07:09:53Z")

</div>

Chris M. wrote:

> Yu Co wrote:
> 
> > data in the form is available under @customer.contact\_detail. But after  
> > submitting the form the contact\_detail dates are not shown but the  
> > customer dates.
> 
> Your view is expecting to find the contact\_detail object in  
> @contact\_detail, but you’re not setting this in the controller code.
> 
> Try adding:
> 
> @contact\_detail = @customer.contact\_detail
> 
> to your register action.
> 
> Chris

Hi Chris,  
many thanks to this advise. Thatâ??s the solution. I supposed that I need  
to change something in the controller and I thought that I even tried  
the suggestion you made. But that was apparently a mistake of mine 🙂 .  
Thnx

See ya

---

<div class="post-metadata">

**Author:** ![yussibaer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/e79b87/32.png) [@yussibaer](https://www.ruby-forum.com/u/yussibaer)\
**Post date:** [October 9, 2006, 10:35am UTC](https://www.ruby-forum.com/t/values-in-form-fields-lost-after-validation/71117/8 "2006-10-09T10:35:21Z")

</div>

Yu Co wrote:

> data in the form is available under @customer.contact\_detail. But after  
> submitting the form the contact\_detail dates are not shown but the  
> customer dates.

Your view is expecting to find the contact\_detail object in  
@contact\_detail, but you’re not setting this in the controller code.

Try adding:

@contact\_detail = @customer.contact\_detail

to your register action.

Chris
