📖 Scenario: You are building a simple login system for a website using Ruby on Rails. You want to remember which user is logged in by using sessions.
🎯 Goal: Build a basic session handling system in Rails that stores a user's ID in the session when they log in and clears it when they log out.
📋 What You'll Learn
Create a controller with a login action that sets the session user ID
Create a variable to hold a fixed user ID for login simulation
Use session hash to store and clear the user ID
Add a logout action that clears the session user ID
💡 Why This Matters
🌍 Real World
Session handling is essential for user login systems on websites to remember who is logged in.
💼 Career
Understanding session management is a key skill for web developers working with Rails or any web framework.
Progress0 / 4 steps
1
Create a SessionsController with a login action
Create a controller called SessionsController with a method login that sets a session key :user_id to 1.
Ruby on Rails
Hint
Use session[:user_id] = 1 inside the login method to store the user ID.
2
Add a fixed user ID variable for login simulation
Inside the SessionsController, create a private method called fixed_user_id that returns 1. Then update the login method to set session[:user_id] to the value returned by fixed_user_id.
Ruby on Rails
Hint
Define a private method fixed_user_id that returns 1. Use it in login to set the session.
3
Add a logout action to clear the session user ID
Add a method logout in SessionsController that removes the :user_id key from the session by setting session[:user_id] = nil.
Ruby on Rails
Hint
Set session[:user_id] = nil inside the logout method to clear the session.
4
Add routes for login and logout actions
In the config/routes.rb file, add routes for login and logout actions in SessionsController using get method. Use get '/login', to: 'sessions#login' and get '/logout', to: 'sessions#logout'.
Ruby on Rails
Hint
Use get '/login', to: 'sessions#login' and get '/logout', to: 'sessions#logout' in routes.rb.
Practice
(1/5)
1. What is the main purpose of using session in a Rails application?
easy
A. To style the webpage dynamically
B. To handle file uploads
C. To connect to the database
D. To store user data across multiple requests
Solution
Step 1: Understand what sessions do
Sessions keep data between different pages or requests for the same user.
Step 2: Identify the correct purpose
Storing user data like login status or preferences is the main use of sessions.
Final Answer:
To store user data across multiple requests -> Option D
Quick Check:
Sessions remember user info across pages = B [OK]
Hint: Sessions keep user info between pages [OK]
Common Mistakes:
Confusing sessions with styling or database connections
Thinking sessions handle file uploads
2. Which of the following is the correct way to store a user's name in the session in Rails?
easy
A. session = { name: 'Alice' }
B. session[:name] = 'Alice'
C. session.name = 'Alice'
D. session('name') = 'Alice'
Solution
Step 1: Recall session syntax in Rails
Sessions use a hash-like syntax with square brackets and symbols for keys.
Step 2: Match the correct syntax
Assigning with session[:name] = 'Alice' is the proper way.
Final Answer:
session[:name] = 'Alice' -> Option B
Quick Check:
Use session[:key] = value syntax = A [OK]
Hint: Use session[:key] = value to store data [OK]
Common Mistakes:
Using assignment with equals on session itself
Trying to call session like a method
Using dot notation which is invalid
3. What will be the output of the following Rails controller code snippet?
If session[:count] is nil or false, it sets it to 0; otherwise, it keeps the value.
Step 2: Increment the count
After setting to 0 if needed, it adds 1, so the value becomes 1.
Final Answer:
1 -> Option C
Quick Check:
Initialize or keep count, then add 1 = 1 [OK]
Hint: ||= sets default, then += increments [OK]
Common Mistakes:
Thinking ||= 0 sets count to 0 every time
Expecting output 0 instead of 1
Assuming session[:count] is nil after increment
4. Identify the error in this Rails controller code that tries to clear the session:
def logout
session.clear()
redirect_to root_path
end
medium
A. No error; code correctly clears the session
B. Missing parentheses in session.clear call
C. session.clear should be session.delete_all
D. session.clear is not a valid method
Solution
Step 1: Check session clearing methods in Rails
Rails session supports clear method to remove all keys.
Step 2: Verify syntax and usage
Calling session.clear() is valid and clears the session.
Final Answer:
No error; code correctly clears the session -> Option A
Quick Check:
session.clear() clears session correctly = D [OK]
Hint: session.clear() empties session data correctly [OK]
Common Mistakes:
Thinking session.clear() is invalid
Confusing with database delete methods
Assuming parentheses are mandatory or cause error
5. You want to store a user's preferred language in the session only if it is not already set. Which code snippet correctly does this in a Rails controller?
hard
A. All of the above
B. session[:language] ||= params[:language]
C. session[:language] = params[:language] if session[:language].nil?
D. session[:language] = params[:language] unless session[:language]
Solution
Step 1: Understand the goal
We want to set session[:language] only if it is not already set.
Step 2: Analyze each option
session[:language] = params[:language] if session[:language].nil? uses if session[:language].nil?, which sets only if nil. session[:language] ||= params[:language] uses ||= which sets if false or nil. session[:language] = params[:language] unless session[:language] uses unless session[:language], which sets if false or nil. All three effectively do the same thing.
Final Answer:
All of the above -> Option A
Quick Check:
All three set only if session[:language] is unset = C [OK]
Hint: ||= or conditional assignment both work [OK]
Common Mistakes:
Thinking only ||= works
Confusing unless with if
Setting session unconditionally
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.