Accessing model's field's across forms

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

Hi Trupti,

Trupti B. 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.


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], {})

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.