Ruby On Rails Classroom image

Anil  Bist / 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
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)