Ruby On Rails Classroom image

Neha  Jaggi / Professional / Web Technology

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

  Current User

Having discussed how to store the user’s remember token in a cookie for later use, we now need to learn how to retrieve the user on subsequent page views. Let’s look again at the sign_in function to see where we are:



Our focus now is the second line:

self . current user = user


  The purpose of this line is to create current_user, accessible in both controllers and views, which will allow constructions such as

<%=  current  %>



redirect  to  current  user  


The use of self is necessary in this context for the same essential reason noted in the discussion leading up to Listing 8.18: without self, Ruby would simply create a local variable called current_user.

                To start writing the code for current_user, note that the line  

self . current  user  =  user


is an assignment, which we must define. Ruby has a special syntax for defining such an assignment function, shown in Listing 8.20.  



This might look confusing—most languages don’t let you use the equals sign in a method definition—but it simply defines a method current_user= expressly designed to handle assignment to current_user. In other words, the code

self .current user = ...


is automatically converted to

current user= ( ... )  


thereby invoking the current_user= method. Its one argument is the right-hand side of the assignment, in this case the user to be signed in. The one-line method body just sets an instance variable @current_user, effectively storing the user for later use.  

Neha  Jaggi

Skills    Ruby On Rails

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

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). 


  Students (0)