Preface - I''ve had a number of posts regarding my bigger issue but trying to break it down into smaller parts. Here is what I''m not understanding regarding REST - why I''m having a hard time passing an instance variable to another action. First I added another resource into my routes. map.resources :searches, :collection => {:userchoices => :get} In the userchoices I hard coded some conditions on the find(:all). I want to make it dynamic though based on the users choice which is getting saved in the create function as @search = Search.new After the save I have { redirect_to userchoices_searches_url(@search)} but no luck in getting the @search params over to the userchoices actions. I guess my real question here is why ? and is there anything I could do to change it. TIA Stuart -- http://en.wikipedia.org/wiki/Dark_ambient --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org
2006-Oct-08 16:15 UTC
Re: Passing instance variables in REST
Hi -- On Sun, 8 Oct 2006, Dark Ambient wrote:> Preface - I''ve had a number of posts regarding my bigger issue but trying to > break it down into smaller parts. > > Here is what I''m not understanding regarding REST - why I''m having a hard > time passing an instance variable to another action. > First I added another resource into my routes. > map.resources :searches, :collection => {:userchoices => :get} > In the userchoices I hard coded some conditions on the find(:all). I want > to make it dynamic though based on the users choice > which is getting saved in the create function as @search = Search.new > > After the save I have { redirect_to userchoices_searches_url(@search)} > but no luck in getting the @search params over to the userchoices actions. > > I guess my real question here is why ? and is there anything I could do to > change it.This isn''t a REST-related thing; it''s true for all actions. A new action, which includes a redirect, starts with a new instance of the controller, which means that its instance variables are not initialized. It''s like doing this: class C def an_action @x = 1 end def other_action puts @x end end C.new.an_action C.new.other_action # nil, not 1 They''re two different instances of C, so they don''t share instance variables. That''s what''s happening with your controller: it''s being instantiated (UserController.new, or equivalent) once for each action. A common technique is to store the id of the result in the session, and then retrieve it on the next action. David -- David A. Black | dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org Author of "Ruby for Rails" [1] | Ruby/Rails training & consultancy [3] DABlog (DAB''s Weblog) [2] | Co-director, Ruby Central, Inc. [4] [1] http://www.manning.com/black | [3] http://www.rubypowerandlight.com [2] http://dablog.rubypal.com | [4] http://www.rubycentral.org --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
On 10/8/06, dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org <dblack-TKXtfPMJ4Ozk1uMJSBkQmQ@public.gmane.org> wrote:> > > Hi -- > > On Sun, 8 Oct 2006, Dark Ambient wrote: > > > Preface - I''ve had a number of posts regarding my bigger issue but > trying to > > break it down into smaller parts. > > > > Here is what I''m not understanding regarding REST - why I''m having a > hard > > time passing an instance variable to another action. > > First I added another resource into my routes. > > map.resources :searches, :collection => {:userchoices => :get} > > In the userchoices I hard coded some conditions on the find(:all). I > want > > to make it dynamic though based on the users choice > > which is getting saved in the create function as @search = Search.new > > > > After the save I have { redirect_to userchoices_searches_url(@search)} > > but no luck in getting the @search params over to the userchoices > actions. > > > > I guess my real question here is why ? and is there anything I could do > to > > change it. > > This isn''t a REST-related thing; it''s true for all actions. A new > action, which includes a redirect, starts with a new instance of the > controller, which means that its instance variables are not > initialized. > > It''s like doing this: > > class C > def an_action > @x = 1 > end > > def other_action > puts @x > end > end > > C.new.an_action > C.new.other_action # nil, not 1 > > They''re two different instances of C, so they don''t share instance > variables. That''s what''s happening with your controller: it''s being > instantiated (UserController.new, or equivalent) once for each action. > > A common technique is to store the id of the result in the session, > and then retrieve it on the next action. > > > DavidThe session sounds like a good way to go. I understand the explanation BUT then if I''m going from the create action to the show action { redirect_to search_url(@search)} that seems to work fine with the id carried over. So how does that get explained ? Stuart --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Preface - I''ve had a number of posts regarding my bigger issue but trying to break it down into smaller parts. Here is what I''m not understanding regarding REST - why I''m having a hard time passing an instance variable to another action. First I added another resource into my routes. map.resources :searches, :collection => {:userchoices => :get} In the userchoices I hard coded some conditions on the find(:all). I want to make it dynamic though based on the users choice which is getting saved in the create function as @search = Search.new After the save I have { redirect_to userchoices_searches_url(@search)} but no luck in getting the @search params over to the userchoices actions. I guess my real question here is why ? and is there anything I could do to change it. TIA Stuart -- http://en.wikipedia.org/wiki/Dark_ambient --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---