# Form Values not Persisting?

**URL:** <https://www.ruby-forum.com/t/form-values-not-persisting/84804>\
**Category:** Rails\
**Created:** [February 7, 2007, 8:39pm UTC](https://www.ruby-forum.com/t/form-values-not-persisting/84804 "2007-02-07T20:39:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [February 7, 2007, 8:39pm UTC](https://www.ruby-forum.com/t/form-values-not-persisting/84804/1 "2007-02-07T20:39:19Z")

</div>

I’m using the text\_field tag to output my form inputs. In the past this  
works perfectly when using model validation. Here’s my problem… a  
3-step checkout process so I’m collecting form data as I go along. So  
after step 1 (billing address), I’m posting the form with a different  
hidden “step” input to the same action.

So the “checkout” action handles all three steps similar to the  
following…

def checkout  
if ! @params[:step]  
render :action =\> “checkout\_billing”  
elsif @params[:step] == “shipping”  
errors = valid\_billing # check the input for required fields  
if errors.size \> 0  
render :action =\> “checkout\_billing”  
else  
render :action =\> “checkout\_shipping”  
end  
elsif @params[:step] == “payment”  
…snip…  
end  
end

So I’m on the checkout\_billing page, I enter my first name and nothing  
else. It will render checkout\_billing again since there are more  
required fields. But the text\_field for billing[first\_name] isn’t  
autopopulated with my first name like the text\_field helper usually  
does.

Any ideas why?

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [February 7, 2007, 10:42pm UTC](https://www.ruby-forum.com/t/form-values-not-persisting/84804/2 "2007-02-07T22:42:48Z")

</div>

the text\_field tag expects an object to exist so that it can use the  
specified attribute as the value for the field

so say i have the following in my ‘new.rhtml’ view:

text\_field :user, :name

i might have in my ‘new’ action

@user = User.new(:name =\> “Billy”)

when the view renders, the field will contain the value ‘Billy’

keeping that in mind, say i submit my form to a ‘create’ action

def create  
@user = User.new(params[:user])  
if @user.save  
redirect\_to :action =\> :list and return  
end  
render :action =\> ‘new’ and return  
end

what happens here is that if there was a missing field in my form for  
whatever reason, i want to render the ‘new’ template and since i  
already instantiated @user, it’s attributes will populate the form  
fields.

in your case, you would need a Billing object created to populate your  
fields, and it doesn’t look like you have that.

---

<div class="post-metadata">

**Author:** ![thebarge](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b38774/32.png) [@thebarge](https://www.ruby-forum.com/u/thebarge)\
**Post date:** [February 7, 2007, 11:11pm UTC](https://www.ruby-forum.com/t/form-values-not-persisting/84804/3 "2007-02-07T23:11:04Z")

</div>

Yeah I poked around looking at my different code when it dawned on me  
that I needed to load the previous screen’s set of data into an object  
so I did that and the default values started showing up.

Thanks.
