Ruby On Rails Classroom image

Neha  Jaggi / Professional / Web Technology

 
To post your Question Join Classroom
 
Lesson Topics's No:-  First|1 | 2 | 3 | 4|
Lessons:- Remember Me

All methods defined in a class after private are automatically hidden, so that

$  rails  console
>>  User .first.create remember token

 

will raise a NoMethodError exception.
                Finally, the create_remember_token method needs to assign to one of the user attributes, and in this context it is necessary to use the self keyword in front of remember_token: 

def    create_remember token
     self . remember token =  SecureRandom .urlsafe base64
end

 

(Note: If you are using Ruby 1.8.7, you should use SecureRandom.hex here instead.) Because of the way Active Record synthesizes attributes based on database columns, without self the assignment would create a local variable called remember_token, which isn’t what we want at all. Using self ensures that assignment sets the user’s remember_token so that it will be written to the database along with the other attributes when the user is saved.
                 Putting this all together yields the User model shown in Listing 8.18. 

 

 

By the way, the extra level of indentation on create_remember_token is there to make it visually apparent which methods are defined after private.

                Since the SecureRandom.urlsafe_base64 string is definitely not blank, the tests for the User model should now be passing:

$  bundle  exec  rspec spec/models/user spec.rb
 
 
 
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)