Form Values not Persisting?


#1

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?


#2

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.


#3

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.