[Ruby on Rails] How to use session method

Introduction

I'm kosuke, a rails beginner. I'm learning rails and the usage of session may be unclear, so I will summarize it as a memorandum.

What is a session?

The session is mainly used for the login function and can keep the login status. By holding a session, you can be treated as a registered user until you log out once you log in. (If you close the browser, it disappears.)

Without session functionality, you would have to keep logging in every time you navigate the page.

In order to implement the session, it is necessary to inform the browser of each user.

How to define

Define a method to grant a session somewhere. In the code below, the user id is given a session named " user_id ".

This will automatically create and save the encrypted user id in the user's browser. By saving the user id on the browser, the user information can be retained.

sessions_helper.rb


#Method to grant session
  def log_in(user)
    session[:user_id] = user.id
  end

The code below is the action when implementing login. If the user's password authentication is correct, I try to give the user a session.

sessions_controller.rb


def create
    user = User.find_by(email: params[:session][:email].downcase)
    if user && user.authenticate(params[:session][:password])
      log_in(user)
      redirect_to user
    else
      flash.now[:danger] = 'Invalid email/password combination'
      render 'new'
    end
  end

How to delete a session

You can delete the session by changing the value of the given session [: user_id] to nil or deleting it.

#Set the value to "nil"
session[:user_id] = nil

Or

#Delete the value
session.delete(:user_id)

How to define current_user

You can use session to return the currently logged in user (if any).

sessions_helper.rb


#Represents the currently logged in user.
def current_user
  if session[:user_id] #user_If you have a session called id, set it to "true"
    User.find_by(id: session[:user_id])
  end
end

How to use the current_user method

ruby:xxx.html.er


#Show the name of the currently logged in user
<%= current_user.name %>

ruby:xxx.html.er


#Display a link to the name and user information of the currently logged in user.
<%= link_to current_user.name, user_path(current_user) %>

References

rails tutorial Chapter 8 Login, logout https://railstutorial.jp/chapters/log_in_log_out?version=4.2

Recommended Posts

[Ruby on Rails] How to use session method
How to use Ruby on Rails
[Ruby on Rails] How to use CarrierWave
[Ruby on Rails] How to use redirect_to
[Ruby on Rails] How to use kaminari
How to use Ruby inject method
[Rails] How to use the map method
[Rails] How to use helper method, confimartion
[Ruby on Rails] "|| =" ← Summary of how to use this assignment operator
[Ruby on Rails] Use the resources method to automatically create routes.
[Ruby on Rails] How to display error messages
How to add / remove Ruby on Rails columns
[Ruby] How to use gsub method and sub method
[Ruby on Rails] How to install Bootstrap in Rails
[Ruby basics] How to use the slice method
[Rails] How to use enum
How to use Ruby return
[Rails] How to use enum
How to use rails join
Ruby: How to use cookies
[Rails] How to use validation
[Rails] How to use authenticate_user!
[Rails] How to use "kaminari"
[Rails] How to use Scope
Method summary to update multiple columns [Ruby on Rails]
[Ruby on Rails] How to write enum in Japanese
[rails] How to use devise helper method before_action: authenticate_user!
[Ruby on Rails] How to change the column name
[Ruby On Rails] How to reset DB in Heroku
(Ruby on Rails6) How to create models and tables
How to use the link_to method
[Rails] How to use gem "devise"
How to deploy jQuery on Rails
How to use the include? method
How to use the form_with method
[Rails] How to use flash messages
[Java] How to use join method
How to use Bio-Formats on Ubuntu 20.04
Rails on Tiles (how to write)
[Rails] How to use Active Storage
[Ruby on Rails] Convenient helper method
[Introduction to Rails] How to use render
How to display a graph in Ruby on Rails (LazyHighChart)
[Ruby On Rails] How to use simple_format to display the entered text with line breaks
Explanation of Ruby on rails for beginners ④ ~ Naming convention and how to use form_Tag ~
A memorandum on how to use Eclipse
How to use Apache Derby on Eclipse
How to use custom helpers in rails
Ruby on Rails installation method [Mac edition]
[Rails] How to use rails console with docker
How to use submit method (Java Silver)
[Rails] How to use ActiveRecord :: Bitemporal (BiTemporalDataModel)
[Java] How to use the toString () method
Ruby length, size, count How to use
How to use MySQL in Rails tutorial
Deploy to Heroku [Ruby on Rails] Beginner
[Ruby] How to use slice for beginners
Preparing to introduce jQuery to Ruby on Rails
[Note] How to use Rails 6 Devise + cancancan
[Rails] How to use video_tag to display videos
[Ruby on Rails] Button to return to top