3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ユーザー新規登録備忘録 part1:複数ページをまたいで行う新規登録の時のデータの流れ~sessionの使い方~

Last updated at Posted at 2019-10-30

やりたいこと

  • 某フリマアプリにて新規登録STEP1~STEP3の3ページで入力した情報を経由してデータベースに出力する
    image01.png
SignupController
def step1
  @user = User.new # 新規インスタンス作成
end

def step2
  # step1で入力された値をsessionに保存
  session[:nick_name] = user_params[:nick_name]
  session[:email] = user_params[:email]
                    [省略]   
  @user = User.new # 新規インスタンス作成
end

def step3
  # step2で入力された値をsessionに保存
  session[:phone_number] = user_params[:phone_number]
  @user = User.new # 新規インスタンス作成
end
                    [省略]   
  • sessionに送るのは最初の2ページだけ!!
nick_name: session[:nick_name], 
email: session[:email],
                      [省略]   
  • 最後のページはuser_paramsで呼び出す
building_name: user_params[:building_name],
phone_number2: user_params[:phone_number2]
                      [省略]   

完成イメージ

image.png

====================================================================================

SignupController完全版
class SignupController < ApplicationController
  def step1
    @user = User.new # 新規インスタンス作成
  end

  def step2
    # step1で入力された値をsessionに保存
    session[:nick_name] = user_params[:nick_name]
    session[:email] = user_params[:email]
                    [省略]  
    @user = User.new # 新規インスタンス作成
  end

  def step3
    # step2で入力された値をsessionに保存
    session[:phone_number] = user_params[:phone_number]
    @user = User.new # 新規インスタンス作成
  end

  def create
    @user = User.new(
    # sessionに保存された値をインスタンスに渡す
    〜〜〜〜1ページ目から2ページまでで入力したデータ〜〜〜〜
      nick_name: session[:nick_name], 
      email: session[:email],
                      [省略]   
      〜〜〜〜3ページ目で入力したデータ〜〜〜〜
      address: user_params[:address],
      building_name: user_params[:building_name],
      phone_number2: user_params[:phone_number2]
                      [省略]
3
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?