# Declaring arrays shared across different controller methods

**URL:** <https://www.ruby-forum.com/t/declaring-arrays-shared-across-different-controller-methods/186591>\
**Category:** Rails\
**Created:** [April 9, 2010, 10:46am UTC](https://www.ruby-forum.com/t/declaring-arrays-shared-across-different-controller-methods/186591 "2010-04-09T10:46:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [April 9, 2010, 10:46am UTC](https://www.ruby-forum.com/t/declaring-arrays-shared-across-different-controller-methods/186591/1 "2010-04-09T10:46:02Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [April 9, 2010, 4:14pm UTC](https://www.ruby-forum.com/t/declaring-arrays-shared-across-different-controller-methods/186591/2 "2010-04-09T16:14:49Z")

</div>

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

> **[Commonthread](https://commonthread.com)**
>
> Instantly connect and build rapport with anyone you meet, in person or online.

---

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [April 12, 2010, 10:48am UTC](https://www.ruby-forum.com/t/declaring-arrays-shared-across-different-controller-methods/186591/3 "2010-04-12T10:48:26Z")

</div>

On 9 April 2010 15:13, Anthony C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 1. Save the previous values in flash and use the keep method to maintain  
> them between requests.
> 2. Use some sort of cache like memcached.

Or  
5) Put them in the database

Colin

---

<div class="post-metadata">

**Author:** ![ESESES](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f0a364/32.png) [@ESESES](https://www.ruby-forum.com/u/ESESES)\
**Post date:** [April 12, 2010, 6:33pm UTC](https://www.ruby-forum.com/t/declaring-arrays-shared-across-different-controller-methods/186591/4 "2010-04-12T18:33:57Z")

</div>

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.
