Forum: Ruby on Rails accessing model's field's across forms

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Cf3efadeb0461f30a12039185319a2dc?d=identicon&s=25 Trupti Bhatt (railsdoubt)
on 2007-02-03 13:07
Hi there,

we have been facing problems while accessing field values.
we have around 30 filelds in our model. so we had created 2 rhtml files
to split the views. now we have submit button on first.if it is being
clicked it should go to the controller. save the fields into temporary
variable & then redirect to the next form. when we click on save button
on the next page the whole(along with the first page) should be saved
into the database. but it will fill null values for the fields in the
previous page.
 how do we access variables declared in controller across multiple
2b891e820c238ded365d035771603f21?d=identicon&s=25 Bill Walton (Guest)
on 2007-02-03 13:29
(Received via mailing list)
Hi Trupti,

Trupti Bhatt wrote:

> now we have submit button on first.if it is being
> clicked it should go to the controller. save the fields
> into temporary variable

A Rails app lives for exactly one request / response cycle.  The only
variables that live longer than that are session variables.  You didn't
why you've decided not to just save the first form's data in the
If the problem is validation-related, you could use
save_with_validation(false).  If that won't work for you, you'll need to
store the first set of form fields to session variables and then
them when you're processing the second form.

Ae97ad0da5c7887be291561eb1720093?d=identicon&s=25 Alex Soto (asoto)
on 2007-02-04 20:54
(Received via mailing list)
I went a different route for our app.  Since we wanted to split the
large amounts of data across mutiple views (like a wizard) but still
wanted to leverage the validations we came up with the following:

We keep the model in the session so it's built up as each step
The basic flow on each step's POST
 - stuff the posted form fields into the model.
 - Call valid on the model
 - if the model isn't valid, extract the model errors that correspond
to the POST'ed form fields
 - redisplay those errors only

On the commit step, the model should be valid so you can commit.

Hopefully the code below can help:

  # This method validates the specified attributes on the model and
  # adds errors for any attributes that fail validation.  Attributes
  # fail validation, but are not in the attributes hash will not be
  # added to the errors.
  # model       a model object to validate
  # attributes  a hash (such as a params hash) that contains
attributes of
  #             the model to validate.
  # errors      a hash to store errors in
  def validate_specified_attributes(model, attributes, errors)
    if !model.valid?
        # if not valid, add error for the specified attributes
        attributes.each do |attr,value|
            errors[attr] = model.errors[attr] unless

In the controller you could call the above as such:
      validate_specified_attributes(@my_model, params[:my_model], {})
21126416f8a3de608f82069ab943a336?d=identicon&s=25 unknown (Guest)
on 2007-02-06 20:54
(Received via mailing list)
A different approach again which heavily customized from this http://

Basically put each page of attributes into a session variable after a
post and validate each step's atributes. Then on the final page which
you do a full validation of the model for safety's sake and then save
the model. Works very well.

Someone has taken this and made plugin out of the main method being
valid_for_attributes/. Though its more flexible without the plugin for

One thing to note, as with any session variable solution, the same
variables are used across multiple browser tabs or windows if the user
has opened your site in them.
This topic is locked and can not be replied to.