Assume that I have a global variable user in application....like this:
# GET /users.xml
def index
@users = User.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @users }
end
end
Is that mean every request, a new @user is created? If every request , an object is created, when will it destroyed? Also, if vistorA go to the web site, a @userA is created, and vistorB go to the web site @userB is created. Will the vistorA have a chance to get the vistorB 's object (@userB)? Also, when will the object release? Thank you.
****Update: the @users is not the global variable, it is a instance variable. So, a question to follow up. How does the server know which @user is belong to which request? Thank you.
@users
is not a global variable it is an instance variable . A new instance of your controller is created to handle each request so the @users
for visitor A and visitor B are independent.
1] @users is not a Global Variable it is a instance variable.its scopre remain upto that method only.
def index
@some_variable= "Hello"
other_method
redirect_to :action=>'redirect_method'
end
def other_method
#here you get @some_variable ="Hello" as you called this method in index where variable is initialise
end
def redirect_method
#here you get @some_variable as you not called this method in index but redirected to this method
end
2] for every user @users will be different as each request handle by server independently
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.