Ruby On Rails Classroom image

Neha  Jaggi / 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
Neha  Jaggi

Skills    Ruby On Rails

Qualifications :- High School - , College/University - Graphic Era Hill University, Dehradun, College/University - ,
Location :-Dehradun,Dehradun,UTTARAKHAND,India
Description:-

Experienced Software Developer with a demonstrated history of working in the Information Technology and services industry. Skilled in Web Technologies (Ruby on Rails, PostgreSQL, php, Laravel and AJAX). 


Explore
 

  Students (0)