Forum: Ruby on Rails Noob Question - Variable Scope

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
7b2a4ea7a2c486b1b0ceab3c6d4680d9?d=identicon&s=25 Joe Cairns (diregnome)
on 2006-05-08 17:59
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?
A7736d622f35f8725cc9a0c835d633a3?d=identicon&s=25 Mathias Wittlock (Guest)
on 2006-05-08 18:05
(Received via mailing list)
On 5/8/06, Joe Cairns <joe.cairns@gmail.com> 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. :)

Mathias.
7b2a4ea7a2c486b1b0ceab3c6d4680d9?d=identicon&s=25 Joe Cairns (diregnome)
on 2006-05-08 18:08
Mathias Wittlock wrote:
> On 5/8/06, Joe Cairns <joe.cairns@gmail.com> 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. :)
>
> Mathias.

Thanks Mathias, that's perfectly clear (at least to me! :D)!
This topic is locked and can not be replied to.