# Variable scope and RESTful methods

**URL:** <https://www.ruby-forum.com/t/variable-scope-and-restful-methods/180840>\
**Category:** Rails\
**Created:** [December 30, 2009, 4:57am UTC](https://www.ruby-forum.com/t/variable-scope-and-restful-methods/180840 "2009-12-30T04:57:19Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [December 30, 2009, 4:57am UTC](https://www.ruby-forum.com/t/variable-scope-and-restful-methods/180840/1 "2009-12-30T04:57:19Z")

</div>

I have the usual methods in a RESTful implementation: index, new

In the index method I have a variable @new\_link which is not nil . At  
the end of the index method, I tested the value with logger.info  
@new\_link.inspect to make sure. And the log output shows that  
@new\_link is in fact not nill.

Within index.html which is displayed by the index method, I include a  
link through new\_link\_path to call the new method to display a form to  
create a new link. I want to prefill this form with the value of  
@new\_link. However, in the scope of the new method, @new\_link is  
nill. How can this be? I thought that @new\_link should have a scope  
that covers the entire instance of the class. Are these two methods  
operate in 2 different instances?

Thanks.

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [December 30, 2009, 1:48pm UTC](https://www.ruby-forum.com/t/variable-scope-and-restful-methods/180840/2 "2009-12-30T13:48:17Z")

</div>

Every time you make a request, ie call a controller action, your rails  
application effectively starts from scratch. There’s no persistent  
instance data between requests, and the reason for this is obvious:  
imagine that you and another person are using the website at the same  
time. If objects persisted between requests, how would the server know  
which one was yours and which was the other persons?

That’s why almost every action uses params[:id] to load an object from  
the db, or uses params from a form to build a new object.

One seeming exception to this is the session, but even this doesn’t  
persist - it uses cookies to get a session key which is used to reload  
the session data from the sessions table in the database.

---

<div class="post-metadata">

**Author:** ![Learn\_by\_D](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/e274bd/32.png) [@Learn\_by\_D](https://www.ruby-forum.com/u/Learn_by_D)\
**Post date:** [December 31, 2009, 12:11am UTC](https://www.ruby-forum.com/t/variable-scope-and-restful-methods/180840/3 "2009-12-31T00:11:01Z")

</div>

Max,

Thank you for the insightful answer. Following your answer, I have  
successfully accomplished my goal by passing value through the session  
variable.

Happy New Year.

Vincent.

On Dec 30, 4:48 am, Max W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## One seeming exception to this is the session, but even this doesn’t persist - it uses cookies to get a session key which is used to reload the session data from the sessions table in the database.
> 
> Posted viahttp://www.ruby-forum.com/.

–

You received this message because you are subscribed to the Google  
Groups “Ruby on Rails: Talk” group.  
To post to this group, send email to removed\_email\_address@domain.invalid.  
To unsubscribe from this group, send email to  
removed\_email\_address@domain.invalid.  
For more options, visit this group at  
[http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).
