Passing instance variables in REST

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

Hi --

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

The 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