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?
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
the session data from the sessions table in the database.
Thank you for the insightful answer. Following your answer, I have
successfully accomplished my goal by passing value through the session
Happy New Year.