Ruby On Rails Classroom image

Anil  Bist / Professional / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2 | 3|Last
Lessons:- Current User

In ordinary Ruby, we could define a second method, current_user, designed to return the value of @current_user, as shown in Listing 8.21.

 

 

If we did this, we would effectively replicate the functionality of attr_accessor, which we saw in Section 4.4.5.5 The problem is that it utterly fails to solve our problem: With the code in Listing 8.21, the user’s signin status would be forgotten. As soon as the user went to another page—poof!—the session would end and the user would be automatically signed out. To avoid this problem, we can find the user corresponding to the remember token created by the code in Listing 8.19, as shown in Listing 8.22.

 

 

Listing 8.22 uses the common but initially obscure ||= (‘‘or equals’’) assignment operator (Box 8.2). Its effect is to set the @current_user instance variable to the user corresponding to the remember token, but only if @current_user is undefined.6 In other words, the construction

@current user ||=  User.find by remember token(cookies  [ :remember token ] )

 

calls the user_from_remember_token method the first time current_user is called, but on subsequent invocations returns @current_user without hitting the database.7 This is only useful if current_user is used more than once for a single user request; in any case, find_by_remember_token will be called at least once every time a user visits a page on the site.  

 
 
 
image
Anil  Bist

Skills    Ruby On Rails

Qualifications :- High School - SLV, College/University - Graphic Era Deemed Univ University,
Location :-Dehradun,Dehradun,Uttarakhand,India
Description:-

I started my Professional Journey in 2006 with one of the Web Development Company in Bangalore and my 1st framework was "Ruby on Rail" as Web development and delivered around 5+ Projects using this platform. Then came another dimension as JEE/Sturst framework, Gradually I realized that I want to build something on my own and give my passion and energy on creating something different a
Explore

 

  Students (0)