Declaring arrays shared across different controller methods

I would like to have an array saving fields to display for an object that is used for its form on multiple pages.

I have declared the array in each of the controller methods that would use it. Where can I put the array so that I only have to define it once and then use it repeatedly on different pages?

ES,

You really can’t do that as you have described because each controller method represents a different http request and a brand new instance of the controller is created. So an array that is saved in a previous request won’t be available in a subsequent request. To accomplish what you want will require persisting the information from previous pages. Some possible solutions…

  1. Save the previous values in hidden form variables each step along the way.

  2. Save the previous values in a session variable.

  3. Save the previous values in flash and use the keep method to maintain them between requests.

  4. Use some sort of cache like memcached.

I hope this helps.

Anthony Crumley

http://commonthread.com

ES, You really can't do that as you have described because each controller method represents a different http request and a brand new instance of the controller is created. So an array that is saved in a previous request won't be available in a subsequent request. To accomplish what you want will require persisting the information from previous pages. Some possible solutions... 1) Save the previous values in hidden form variables each step along the way. 2) Save the previous values in a session variable. 3) Save the previous values in flash and use the keep method to maintain them between requests. 4) Use some sort of cache like memcached.

Or 5) Put them in the database

Colin

ES wrote:

I would like to have an array saving fields to display for an object that is used for its form on multiple pages.

I have declared the array in each of the controller methods that would use it. Where can I put the array so that I only have to define it once and then use it repeatedly on different pages?

You could always render that information in a partial so your object always appears the same to the user no matter what form they are on.

Next implement view caching on that partial. No need to clutter up session, pass around hidden fields, or necessarily hit the DB for each display, just render the cached partial.