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 –

On Sun, 8 Oct 2006, Dark A. wrote:

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 | [email protected]
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

On 10/8/06, [email protected] [email protected] wrote:

Here is what I’m not understanding regarding REST - why I’m having a
but no luck in getting the @search params over to the userchoices

end

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs