Forum: Ruby on Rails Form Values not Persisting?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
29d82d169c6037d4cbafce0b20fcf04e?d=identicon&s=25 The Barge (thebarge)
on 2007-02-07 20:39
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?
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (au5lander)
on 2007-02-07 22:42
(Received via mailing list)
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.
29d82d169c6037d4cbafce0b20fcf04e?d=identicon&s=25 The Barge (thebarge)
on 2007-02-07 23:11
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.
This topic is locked and can not be replied to.