Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add user authentication in a Rails controller.
Ruby on Rails
before_action :[1], only: [:edit, :update, :destroy] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using authorization methods instead of authentication
Forgetting the exclamation mark in method name
✗ Incorrect
The authenticate_user! method ensures only logged-in users can access certain actions.
2fill in blank
mediumComplete the code to create a secure login form in Rails.
Ruby on Rails
<%= form_with url: session_path, method: :post do |[1]| %> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names that are not standard form builders
Using plural or unrelated variable names
✗ Incorrect
The variable f is commonly used as the form builder in Rails forms.
3fill in blank
hardFix the error in the authentication check method.
Ruby on Rails
def current_user @current_user ||= User.find_by(id: session[:[1]]) end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'id' instead of 'user_id'
Using 'session_id' which is unrelated
✗ Incorrect
The session stores the user_id to identify the logged-in user.
4fill in blank
hardFill both blanks to securely log out a user in a Rails controller.
Ruby on Rails
def destroy session[:[1]] = [2] redirect_to root_path end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting session key to false instead of nil
Using wrong session key name
✗ Incorrect
Setting session[:user_id] to nil logs out the user by clearing their session.
5fill in blank
hardFill all three blanks to create a secure user authentication helper method.
Ruby on Rails
def logged_in? !!session[:[1]] && User.exists?(id: session[:[2]]) && @current_user = User.find_by(id: session[:[3]]) end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different keys for session lookup
Using 'current_user_id' which is not standard
✗ Incorrect
All session references must use user_id to check and find the logged-in user.