Noob Question - Variable Scope


#1

I feel awful asking such a basic question, but here it goes…

I have a controller, “NewsController”.

The user enters and the “list” action is called and I populate an
instance variable “@news” which contains all the elements I’d like to
display using a form.

The user then changes something and the “update” action is called.

Is “@news” still in scope? When is “@news” out of scope and thus
destroyed?


#2

On 5/8/06, Joe C. removed_email_address@domain.invalid wrote:

Is “@news” still in scope? When is “@news” out of scope and thus
destroyed?

If I understand you correctly, you have a user going to a “list” page
to see all the news and then deciding to click a link to update a news
item on that list? The @news item is only good from when it’s created
in the controller until the view has been completely rendered. So when
the next action (update) is called, it’s no longer around. Instead
you’ll need to create it again, or you could pass some relevant
information to the update action (if you only want it to find which
news item the user wants to update, you could pass that item’s ID to
the update action).

I hope I’m making any sense here, otherwise I’m sure someone else will
come in and clearify it for you. :slight_smile:

Mathias.


#3

Mathias W. wrote:

On 5/8/06, Joe C. removed_email_address@domain.invalid wrote:
If I understand you correctly, you have a user going to a “list” page
to see all the news and then deciding to click a link to update a news
item on that list? The @news item is only good from when it’s created
in the controller until the view has been completely rendered. So when
the next action (update) is called, it’s no longer around. Instead
you’ll need to create it again, or you could pass some relevant
information to the update action (if you only want it to find which
news item the user wants to update, you could pass that item’s ID to
the update action).

I hope I’m making any sense here, otherwise I’m sure someone else will
come in and clearify it for you. :slight_smile:

Mathias.

Thanks Mathias, that’s perfectly clear (at least to me! :D)!