rhubarb
2006-Mar-13 09:38 UTC
[Rails] lame newbie question: how to pass instance variables
This should be obvious - but I''m a long-time non-web app programmer and new to rails. I simply want to put something in a variable in one action and access it in another. It''s like this, I''m doing a simple photo gallery/managment app, and I have two actions in my picture_controller: The first one scans the picture directory and creates a new object in the pictures table for each picture it finds. The work is all done in a method in the Picture model which returns an array containing the new picture objects. The second method is a normal list method, but instead of getting it''s data from the db, gets it from that scan method - because it only lists newly found pictures. At least that''s the plan. How do I pass the array found in the first action to the second one. So in the action we have something like this: def scan_directory # new_pics is an instance variable I''m creating here - I want # to access it in my other action to get only the new pictures. # There''s nothing in the database record to indicate what''s new, it''s # just a question of which new pics I found when calling scan_directory @new_pics = Picture.load_directory(".") flash[:notice] = "Original image directory was refreshed #{@new_pics.length} new images found" redirect_to :controller => ''picture'', :action => ''list_new_pictures'' end def list_new_pictures # Is this how I should use a paginator when my list is not from the db? # Note that I get an exception here, because @new_pics is always nil! @picture_pages = Paginator.new self, @new_pics.count, 12, @params[''page''] @pictures = @new_pics render :action => ''list'' end So what am I doing wrong? Is it not right to expect that instance variables to hold values across actions? Why not? Should I be shoving the @new_pics array into one of the hashes that gets passed back and forth between the view and controller? Is my paginator correct? any help appreciated -- Posted via http://www.ruby-forum.com/.
bump - simple question - someone must know. -- Posted via http://www.ruby-forum.com/.
Alex Young
2006-Mar-13 11:37 UTC
[Rails] lame newbie question: how to pass instance variables
rhubarb wrote:> So what am I doing wrong? Is it not right to expect that instance > variables to hold values across actions? Why not?They don''t hold their values across actions because Rails'' execution model dictates that each action takes place in a new instance of the controller. There are two ways to get what you''re trying to do to work. The first is to call render rather than redirect_to in the scan_directory action. That would call the list_new_pictures method directly in the same controller, so the instance variable would be available. The second would be to put your list of pictures into either the session or the params hash before issuing the redirect, then fishing it out again in the list_new_pictures action. I''d tend towards the former, because it''s less code. I can''t comment on the paginator, because I don''t use it much... -- Alex
rhubarb
2006-Mar-13 15:16 UTC
[Rails] Re: lame newbie question: how to pass instance variables
Alex Young wrote:> > There are two ways to get what you''re trying to do to work. The first > is to call render rather than redirect_to in the scan_directory action. > That would call the list_new_pictures method directly in the same > controller, so the instance variable would be available. The second > would be to put your list of pictures into either the session or the > params hash before issuing the redirect, then fishing it out again in > the list_new_pictures action. > > I''d tend towards the former, because it''s less code. > > I can''t comment on the paginator, because I don''t use it much...That''s exactly what I needed to know. That controller is reinstantiated for every action call is a key point that I never noticed in any of the docs. Thanks Alex! -- Posted via http://www.ruby-forum.com/.
Nicolas Buet
2006-Mar-13 19:21 UTC
[Rails] lame newbie question: how to pass instance variables
you can put it in session, params or flash (the last one was missing). flash is cleared after a page rendering, and this is why I use it rather than the others. On 3/13/06, Alex Young <alex@blackkettle.org> wrote:> > rhubarb wrote: > > So what am I doing wrong? Is it not right to expect that instance > > variables to hold values across actions? Why not? > They don''t hold their values across actions because Rails'' execution > model dictates that each action takes place in a new instance of the > controller. > > There are two ways to get what you''re trying to do to work. The first > is to call render rather than redirect_to in the scan_directory action. > That would call the list_new_pictures method directly in the same > controller, so the instance variable would be available. The second > would be to put your list of pictures into either the session or the > params hash before issuing the redirect, then fishing it out again in > the list_new_pictures action. > > I''d tend towards the former, because it''s less code. > > I can''t comment on the paginator, because I don''t use it much... > > -- > Alex > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060313/e2b7b9b5/attachment.html
rhubarb
2006-Mar-13 20:51 UTC
[Rails] Re: lame newbie question: how to pass instance variables
Nicolas Buet wrote:> you can put it in session, params or flash (the last one was missing). > flash > is cleared after a page rendering, and this is why I use it rather than > the > others.actually I''ve decided against render because I want to change the URL. So I''m left with one of these. so how do I put it in, say, the params. I tried @params[:new_pictures] = new_pics and later in a different action new_pics = @params[:new_pictures] but I get a nil error. What am I doing wrong. Also that Pagination trick I tried above isnt working - Im getting everything in one page. Ideas? -- Posted via http://www.ruby-forum.com/.
Ezra Zygmuntowicz
2006-Mar-13 21:58 UTC
[Rails] Re: lame newbie question: how to pass instance variables
On Mar 13, 2006, at 12:50 PM, rhubarb wrote:> Nicolas Buet wrote: >> you can put it in session, params or flash (the last one was >> missing). >> flash >> is cleared after a page rendering, and this is why I use it rather >> than >> the >> others. > > actually I''ve decided against render because I want to change the URL. > So I''m left with one of these. > so how do I put it in, say, the params. I tried > @params[:new_pictures] = new_pics > > and later in a different action > > new_pics = @params[:new_pictures] > > but I get a nil error. > What am I doing wrong. > > Also that Pagination trick I tried above isnt working - Im getting > everything in one page. Ideas? > > --Lets break this down a bit for you. Lets assume you want to redirect from action foo to action bar and keep something in the params along the way: def foo # the rest of your code params[:new_pictures] = new_pics redirect_to :action => ''bar, :params => params end def bar # now params[:new_pictures] has what you want it to have. end -Cheers -Ezra Zygmuntowicz Yakima Herald-Republic WebMaster http://yakimaherald.com 509-577-7732 ezra@yakima-herald.com