Is there any way to get several view "renders" from a single controller? Say I have an expensive action that does a lot of processing to create an object. I then want to view various aspects of this object, but it makes sense for these views to be on separate pages. class some_controller def view_object @big_object = get_object end private def get_object #lots of processing... return object end end view_object.rhtml displays one aspect of @big_object. Is it possible to go to another view, or render a file/template while still using the same @big_object instance? I''d like something the user could "click on" that would render a new view without doing all the controller processing agin - I just want to use the same controller since I already have the object I want. It seems like it should be possible, but I can''t tear myself away from the "trip to the server" mentality. I could just stuff @big_object into the session, then execute a controller action that does nothing except render a new view, but I was wondering if there is a nicer way of doing this. Any insight?
Hi Just a wild guess: Have you looked at partials and/or components? Regards, Manuel
Julian ''Julik'' Tarkhanov
2005-Oct-05 10:37 UTC
Re: Change views without a new transactions
Why not to store the object in the session?'' before_filter :compute_object def expensive_object_compute session[:big_foo] ||= compute_foo() end dev view @expensive_monster = session[:big_foo] # show stuff end and then recompute this object in the session when necessary? On 5-okt-2005, at 6:09, Matthew Thill wrote:> Is there any way to get several view "renders" from a single > controller? Say I have an expensive action that does a lot of > processing to create an object. I then want to view various aspects > of this object, but it makes sense for these views to be on > separate pages. > > class some_controller > > def view_object > @big_object = get_object > end > > private > > def get_object > #lots of processing... > return object > end > > end > > view_object.rhtml displays one aspect of @big_object. Is it > possible to go to another view, or render a file/template while > still using the same @big_object instance? I''d like something the > user could "click on" that would render a new view without doing > all the controller processing agin - I just want to use the same > controller since I already have the object I want. It seems like it > should be possible, but I can''t tear myself away from the "trip to > the server" mentality. > > I could just stuff @big_object into the session, then execute a > controller action that does nothing except render a new view, but I > was wondering if there is a nicer way of doing this. > > Any insight?-- Julian "Julik" Tarkhanov
Manuel Holtgrewe wrote:> Hi > > Just a wild guess: Have you looked at partials and/or components?Yes, I''ve looked at partials. I use them all over in my current application, but I can''t figure out a way to render one without a trip to the server. As I mentioned in reply to the other reply, I''ve done some thinking and what I want just doesn''t make sense - the controller does not persist once the view has been rendered. Anything I want to keep around, I need to store in the session. Thanks for the reply.> > > Regards, > > Manuel > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Julian ''Julik'' Tarkhanov wrote:> Why not to store the object in the session?''This is the type of solution I was refering to in my last paragraph. I was wondering if there was a different way, but I now don''t think there is. At the end of a transaction, the controller renders the view and then its life is over. There is no "persistant controller" to refer to any more. I was concerned about memory issues by stuffing big objects in the session, but now that I think about it, keeping the controller around would require even more memory. Thanks for the reply.> > before_filter :compute_object > > def expensive_object_compute > session[:big_foo] ||= compute_foo() > end > > dev view > @expensive_monster = session[:big_foo] > # show stuff > end > > and then recompute this object in the session when necessary? > > On 5-okt-2005, at 6:09, Matthew Thill wrote: > >> Is there any way to get several view "renders" from a single >> controller? Say I have an expensive action that does a lot of >> processing to create an object. I then want to view various aspects >> of this object, but it makes sense for these views to be on separate >> pages. >> >> class some_controller >> >> def view_object >> @big_object = get_object >> end >> >> private >> >> def get_object >> #lots of processing... >> return object >> end >> >> end >> >> view_object.rhtml displays one aspect of @big_object. Is it possible >> to go to another view, or render a file/template while still using >> the same @big_object instance? I''d like something the user could >> "click on" that would render a new view without doing all the >> controller processing agin - I just want to use the same controller >> since I already have the object I want. It seems like it should be >> possible, but I can''t tear myself away from the "trip to the server" >> mentality. >> >> I could just stuff @big_object into the session, then execute a >> controller action that does nothing except render a new view, but I >> was wondering if there is a nicer way of doing this. >> >> Any insight? > > >
On 10/5/05, Matthew Thill <mithill-sK6dKysfGH7D0D/r9Z6QQA@public.gmane.org> wrote:> I was concerned about memory issues by stuffing big objects in the > session, but now that I think about it, keeping the controller around > would require even more memory.I don''t know how big is "big", or how expensive is "expensive", but you could create a table specifically to hold serialized copies of these "big" objects, and then just store the id in the session. You could then age them and delete old ones from the table, even in the controller, or in a separate housekeeping routine. If the serialized object is stored in the db, you can retrieve it without putting a big object into the session data so that it doesn''t have to get transferred back and forth to the client.
Ryan Waldron wrote:> On 10/5/05, Matthew Thill <mithill-sK6dKysfGH7D0D/r9Z6QQA@public.gmane.org> wrote: > > >>I was concerned about memory issues by stuffing big objects in the >>session, but now that I think about it, keeping the controller around >>would require even more memory. > > > I don''t know how big is "big", or how expensive is "expensive", but > you could create a table specifically to hold serialized copies of > these "big" objects, and then just store the id in the session. You > could then age them and delete old ones from the table, even in the > controller, or in a separate housekeeping routine.That is a very good idea. Thanks for the tip.> > If the serialized object is stored in the db, you can retrieve it > without putting a big object into the session data so that it doesn''t > have to get transferred back and forth to the client. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails