Complete the code to set a session value in a Rails controller.
session[:user_id] = [1]In Rails, session values are usually set from parameters received in the request, such as params[:user_id].
Complete the code to retrieve the current user ID from the session in a Rails controller.
current_user_id = session[1]Session keys in Rails are accessed using symbols inside square brackets, like session[:user_id].
Fix the error in the code to clear the session in a Rails controller.
def logout [1] redirect_to root_path end
To clear all session data in Rails, use session.clear. Other methods either delete a single key or do not exist.
Fill both blanks to check if a user is logged in by verifying the session key.
if session[1] && session[2] != nil # user is logged in end
To check if a user is logged in, verify the presence of session[:user_id]. Using symbol keys consistently is best practice.
Fill all three blanks to create a session hash storing user name and role if the user is admin.
if params[:role] == 'admin' session[1] = params[:name] session[2] = params[:role] flash[:notice] = 'Admin logged in as ' + session[3] end
Store the user's name in session[:user_name] and role in session[:user_role]. Use the same key to display the name in the flash message.