Action Variables Not Available In The View For A Specific Ac


#1

I am setting an action variable in my PagesController such as:

def manage
render(:layout => “admin”)
@strOut = “STRING Variable Set From The MANAGE Action!”
end

And in the /views/pages/manage.rhtml view, I don?t have access to that
@strOut variable by trying to call it as: <%= @strOut %>

However, I am setting the same variable in another action in the
controller
such as:

def view
@strOut = “STRING Variable Set From The EDIT Action!”
end

… and I have full access to it in /views/pages/view.rhtml

The only difference in the actions being this:
render(:layout => “admin”) …in the manage action.

So, I want the manage action to use /views/layouts/admin.rhtml, and the
edit
action to use /views/layouts/pages.rhtml.

I’m not sure if the layouts have anything to do with it but I really
don’t
know why that variable cannot be called from the view.

Jeff


#2

It’s an order thing.

render :layout => ‘admin’ actually copies the instance variables from
the controller for use in the template. It’s actually a bit more
complicated than that but that’s the just. In otherwards, at the time
the instance variables are copied to the template, your instance
variable @strOut does not exist and is not copied. Just code your
method like:

def manage
@strOut = “BLAH”
render :action => ‘admin’
end

and all should be good.

-Matt


#3

Excellent, re-ordering them worked. Thanks very much.

Jeff


#4

On May 10, 2006, at 10:31 AM, Jeff W. wrote:

I am setting an action variable in my PagesController such as:

def manage
render(:layout => “admin”)
@strOut = “STRING Variable Set From The MANAGE Action!”
end

And in the /views/pages/manage.rhtml view, I don?t have access to that
@strOut variable by trying to call it as: <%= @strOut %>

You set the variable after the view was rendered. Swap those two lines.

jeremy