accessing model's field's across forms

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

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.

A different approach again which heavily customized from this http:// www.bigbold.com/snippets/posts/show/277

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