0
0
Ruby on Railsframework~20 mins

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

Choose your learning style9 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.