0
0
Ruby on Railsframework~10 mins

Login and logout flow in Ruby on Rails - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new session for a user after login.

Ruby on Rails
session[:user_id] = [1]
Drag options to blanks, or click blank then click option'
AUser.find(params[:id])
Bcurrent_user
C@user.id
Dparams[:user_id]
Attempts:
3 left
💡 Hint
Common Mistakes
Using the whole user object instead of just the ID.
Trying to store params[:user_id] which may not be set.
2fill in blank
medium

Complete the code to find the current logged-in user from the session.

Ruby on Rails
@current_user ||= User.find_by(id: [1])
Drag options to blanks, or click blank then click option'
Asession[:user_id]
Bparams[:user_id]
Ccookies[:user_id]
Drequest.user_id
Attempts:
3 left
💡 Hint
Common Mistakes
Using params instead of session to find the user.
Trying to find user by cookies or request which are not used here.
3fill in blank
hard

Fix the error in the logout method to clear the user session.

Ruby on Rails
def logout
  [1]
  @current_user = nil
end
Drag options to blanks, or click blank then click option'
Asession.delete(:user_id)
Breset_session
Csession[:user_id] = nil
Dsession.clear
Attempts:
3 left
💡 Hint
Common Mistakes
Using session.clear which removes all session data, not just user ID.
Setting session[:user_id] to nil does not remove the key.
4fill in blank
hard

Fill both blanks to create a login form that posts to the sessions path with email and password fields.

Ruby on Rails
<%= form_with url: [1], method: [2] do |form| %>
  <%= form.label :email %>
  <%= form.email_field :email %>
  <%= form.label :password %>
  <%= form.password_field :password %>
  <%= form.submit "Log in" %>
<% end %>
Drag options to blanks, or click blank then click option'
Alogin_path
Bsessions_path
C:post
D:get
Attempts:
3 left
💡 Hint
Common Mistakes
Using GET method which is not secure for passwords.
Using login_path which may not be defined for sessions.
5fill in blank
hard

Fill all three blanks to implement a before_action that requires login for certain controller actions.

Ruby on Rails
before_action :[1], only: [:edit, :update, :destroy]

private

def [1]
  redirect_to [2] unless [3]
end
Drag options to blanks, or click blank then click option'
Arequire_login
Blogin_path
Clogged_in?
Dauthenticate_user
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching method names between before_action and private method.
Redirecting to wrong path or missing the logged_in? check.