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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Anthony Crumley
2010-Apr-09 14:13 UTC
Re: Declaring arrays shared across different controller methods
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 On Fri, Apr 9, 2010 at 3:45 AM, ES <emstolfo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Colin Law
2010-Apr-12 08:46 UTC
Re: Declaring arrays shared across different controller methods
On 9 April 2010 15:13, Anthony Crumley <anthony.crumley-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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> I hope this helps. > Anthony Crumley > http://commonthread.com > > On Fri, Apr 9, 2010 at 3:45 AM, ES <emstolfo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 received this message because you are subscribed to the Google Groups >> "Ruby on Rails: Talk" group. >> To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> To unsubscribe from this group, send email to >> rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org >> For more options, visit this group at >> http://groups.google.com/group/rubyonrails-talk?hl=en. >> > > -- > You received this message because you are subscribed to the Google Groups > "Ruby on Rails: Talk" group. > To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to > rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. >-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Ar Chron
2010-Apr-12 16:33 UTC
Re: Declaring arrays shared across different controller methods
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. -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.