Bird
Raised Fist0
Ruby on Railsframework~20 mins

Session handling in Ruby on Rails - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Session Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What is the output of this Rails controller action regarding session?

Consider this Rails controller action:

def login
  session[:user_id] = 42
  render plain: session[:user_id]
end

What will be the response body when this action is called?

Ruby on Rails
def login
  session[:user_id] = 42
  render plain: session[:user_id]
end
A"nil"
B"42"
C"session[:user_id]"
D"user_id"
Attempts:
2 left
💡 Hint

Remember, session stores values as key-value pairs accessible by keys.

❓ state_output
intermediate
2:00remaining
What is the session content after this sequence?

Given this sequence in a Rails controller:

session[:cart] = ['apple']
session[:cart] << 'banana'
session[:cart].delete('apple')

What is the value of session[:cart] after these lines?

Ruby on Rails
session[:cart] = ['apple']
session[:cart] << 'banana'
session[:cart].delete('apple')
A['banana']
B['apple', 'banana']
C[]
D['apple']
Attempts:
2 left
💡 Hint

Think about how arrays change with << and delete methods.

📝 Syntax
advanced
2:00remaining
Which option correctly sets a flash message in Rails session?

Which of these lines correctly sets a flash notice message in a Rails controller?

Asession[:flash] = "Welcome!"
Bsession.flash[:notice] = "Welcome!"
Cflash.notice = "Welcome!"
Dflash[:notice] = "Welcome!"
Attempts:
2 left
💡 Hint

Flash messages use a special hash called flash.

🔧 Debug
advanced
2:00remaining
Why does this session assignment not persist across requests?

In a Rails controller, this code is used:

def set_user
  user = User.find(params[:id])
  session[:user] = user
  render plain: "User set"
end

Why might session[:user] not persist correctly across requests?

Ruby on Rails
def set_user
  user = User.find(params[:id])
  session[:user] = user
  render plain: "User set"
end
ABecause User.find returns nil if user not found, causing session to clear
BBecause session[:user] key is reserved and cannot be used
CBecause session can only store simple data types, not ActiveRecord objects
DBecause render plain disables session storage
Attempts:
2 left
💡 Hint

Think about what kind of data can be stored in session.

🧠 Conceptual
expert
2:00remaining
What is the main security risk of storing sensitive data in Rails session?

Why is it risky to store sensitive information like passwords or credit card numbers directly in Rails session?

ABecause session data is stored client-side and can be tampered with if not encrypted
BBecause sessions automatically expire after 5 minutes, losing data
CBecause Rails encrypts session data making it impossible to read later
DBecause storing data in session causes server crashes under load
Attempts:
2 left
💡 Hint

Consider where session data is stored and how it can be accessed.

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

  1. Step 1: Understand what sessions do

    Sessions keep data between different pages or requests for the same user.
  2. Step 2: Identify the correct purpose

    Storing user data like login status or preferences is the main use of sessions.
  3. Final Answer:

    To store user data across multiple requests -> Option D
  4. 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

  1. Step 1: Recall session syntax in Rails

    Sessions use a hash-like syntax with square brackets and symbols for keys.
  2. Step 2: Match the correct syntax

    Assigning with session[:name] = 'Alice' is the proper way.
  3. Final Answer:

    session[:name] = 'Alice' -> Option B
  4. 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?
session[:count] ||= 0
session[:count] += 1
render plain: session[:count]
medium
A. nil
B. 0
C. 1
D. Error

Solution

  1. Step 1: Understand the ||= operator

    If session[:count] is nil or false, it sets it to 0; otherwise, it keeps the value.
  2. Step 2: Increment the count

    After setting to 0 if needed, it adds 1, so the value becomes 1.
  3. Final Answer:

    1 -> Option C
  4. 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

  1. Step 1: Check session clearing methods in Rails

    Rails session supports clear method to remove all keys.
  2. Step 2: Verify syntax and usage

    Calling session.clear() is valid and clears the session.
  3. Final Answer:

    No error; code correctly clears the session -> Option A
  4. 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

  1. Step 1: Understand the goal

    We want to set session[:language] only if it is not already set.
  2. 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.
  3. Final Answer:

    All of the above -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard