# Accessing model's field's across forms

**URL:** <https://www.ruby-forum.com/t/accessing-models-fields-across-forms/84347>\
**Category:** Rails\
**Created:** [February 3, 2007, 1:07pm UTC](https://www.ruby-forum.com/t/accessing-models-fields-across-forms/84347 "2007-02-03T13:07:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![truptibhatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8c91f0/32.png) [@truptibhatt](https://www.ruby-forum.com/u/truptibhatt)\
**Post date:** [February 3, 2007, 1:07pm UTC](https://www.ruby-forum.com/t/accessing-models-fields-across-forms/84347/1 "2007-02-03T13:07:04Z")

</div>

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  
function.

---

<div class="post-metadata">

**Author:** ![truptibhatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8c91f0/32.png) [@truptibhatt](https://www.ruby-forum.com/u/truptibhatt)\
**Post date:** [February 3, 2007, 1:29pm UTC](https://www.ruby-forum.com/t/accessing-models-fields-across-forms/84347/2 "2007-02-03T13:29:30Z")

</div>

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  
say  
why you’ve decided not to just save the first form’s data in the  
database.  
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  
retrieve  
them when you’re processing the second form.

hth,  
Bill

---

<div class="post-metadata">

**Author:** ![truptibhatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8c91f0/32.png) [@truptibhatt](https://www.ruby-forum.com/u/truptibhatt)\
**Post date:** [February 4, 2007, 8:54pm UTC](https://www.ruby-forum.com/t/accessing-models-fields-across-forms/84347/3 "2007-02-04T20:54:14Z")

</div>

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  
completes.  
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

that

# 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  
model.errors[attr].nil?  
end  
end  
end

In the controller you could call the above as such:  
validate\_specified\_attributes(@my\_model, params[:my\_model], {})

---

<div class="post-metadata">

**Author:** ![truptibhatt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8c91f0/32.png) [@truptibhatt](https://www.ruby-forum.com/u/truptibhatt)\
**Post date:** [February 6, 2007, 8:54pm UTC](https://www.ruby-forum.com/t/accessing-models-fields-across-forms/84347/4 "2007-02-06T20:54:05Z")

</div>

A different approach again which heavily customized from this http://

> **[BigBold - Informasi Tentang Bisnis dan Marketing](http://www.bigbold.com)**
>
> BigBold ialah situs yang memberikan segala macam Informasi Tentang Bisnis dan Marketing

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 [http://svn.ardes.com/ardes/rails/plugins/](http://svn.ardes.com/ardes/rails/plugins/)  
valid\_for\_attributes/. Though its more flexible without the plugin for  
me.

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.
