[Rails] Temporary retention of data by session

Introduction

session is used when moving to the transition destination while holding data in "wizard format" etc.

table of contents

  1. About the session
  2. Implementation example
  3. Finally

1. About the session

A session is a mechanism for temporarily storing information. In Rails, a session stores data in an object called session in the form of a hash.

For example, suppose a site where the page changes when you register as a member. In this case, it is necessary to temporarily store the information entered using the session in the session, move to the next page, and expand it there.

2 Implementation example

Premise

As a premise, it is assumed that member information is saved while transitioning pages using devise.

Development environment

ruby 2.6.5 rails 6.0.0 devise 4.7.3

2.1 Data retention

Let's implement it!

registrations_controller.rb


class Users::RegistrationsController < Devise::RegistrationsController

  def new
    @user = User.new
  end

  def create
    @user = User.new(sign_up_params)
      unless @user.valid?
        render :new and return
      end
    session["devise.regist_data"] = {user: @user.attributes}
    @address = @user.build_address
    render "new_address"
  end

I have written various other things, but I will pay attention to session.

session["devise.regist_data"] = {user: @user.attributes} 

{user: @ user.attributes} uses the attributes method to format the data when you want session to hold information in the form of a hash object.

In other words, the attributed @user data is in the session in a hashed state. Now, the input information is assigned to session ["devise.regist_data"] and it is in the retained state.

2.2 Deploy

Next, the page is transitioned and expanded while retaining the data.

registrations_controller.rb


class Users::RegistrationsController < Devise::RegistrationsController

#abridgement
 def create_address
    @user = User.new(session["devise.regist_data"]["user"])
    @address = Address.new(useraddress_params)
      if @address.valid?
        @user.build_address(@address.attributes)
        @user.save
          session["devise.regist_data"]["user"].clear
          sign_in(:user, @user)
      else
        render "new_address"
      end
  end

This is also described in various ways, but pay attention to @user at the top.

@user = User.new(session["devise.regist_data"]["user"])

Describes session ["devise.regist_data"] ["user"]. If you have data with {user: 〇〇}, you need to use ["user"]. Also, since session contains information in an array, you can also have information as follows. session["devise.regist_data"] = {user: @user.attributes, address: @user.address}

With this, session can be expanded on the transition destination page and assigned to @user.

After that, you can save using the data assigned to @user.

Summary

This time, I introduced how to use session when using devise in "wizard format".

Finally

I am a beginner in programming, but I am posting an article in the hope that it will help people who are similarly troubled. See you next time ~

Recommended Posts

[Rails] Temporary retention of data by session
[Rails] Introduction of Rubocop by beginners
[Ruby on Rails] Introduction of initial data
The contents of the data saved by CarrierWave.
[Order method] Set the order of data in Rails
Implementation of Ruby on Rails login function (Session)
Summary of basic knowledge of Rails acquired by progate
[Rails] Display of multi-layered data using select boxes
A review of the code used by rails beginners
[Rails] Addition of columns / Change of data type / column name
Rails6: Input the initial data of ActionText using seed
[Rails 6] Validation by belongs_to
[Rails] Introduction of PAY.JP
Rails Tutorial/Significance of Indexing
[Beginner] About Rails Session
The story of managing session information by introducing gem Redis
[Rails] Register by attribute of the same model using Devise
Memo of parsing by SAX parser of RDF / XML data model
[Rails] How to display the list of posts by category