6時だョ!!全員集合!!

Rails・JavaScrictを中心にアウトプットします。

2022年5月19日 ログイン機能 ・サインアップ機能を作成

テーブル構成

◆ usersテーブル

name:string email: string crypted_password: string salt: string

◆ postsテーブル

title:string body: text user_id:bigint(references)

◆ commentsテーブル

body: text text: string

実装

ログイン機能・サインアップ機能をsorceryで実装しました。
Simple Password Authentication

実装を通した気づき

Yano
  • debuggerで値の中身を確認することが出来ます。
  • "@user"やparamsと叩くなどして、どういったパラメータを受け取っているかなどでミスを発見しました。
[2, 11] in /Users/yanokouhei/workspace/rails-free/free/app/controllers/user_sessions_controller.rb
    2:   skip_before_action :require_login, only: [:new, :create]
    3: 
    4:   def create
    5:     @user = login(params[:email], params[:password])
    6:     debugger
=>  7:     if @user
    8:       redirect_back_or_to(:users, notice: 'Login successful')
    9:     else
   10:       flash.now[:alert] = 'Login failed'
   11:       render action: 'new'
(byebug) @user
#<User id: 3, email: "yano2@yano", crypted_password: "$2a$10$4TIvRfrA3S2NlbfGzkrs/.zr2hjnzjh2UYKfGMl3VFe...", salt: "_PMqGaxonoZgeF8M5k1z", created_at: "2022-05-19 00:33:14", updated_at: "2022-05-19 00:33:14">

コロンの位置などは注意した方が良いです。

  has_many :posts, dependent: :destroy
Yui
Yuki
  • scaffoldで生成したファイルに処理内容が自動的に書かれているのがすごいと感じました。
  • strongパラメータにちゃんと値が渡っているか気をつけるべきです。
  def user_params
    params.require(:user).permit(:email, :password, :password_confirmation)
  end

明日やること

ユーザーに紐付けた投稿機能やります!

参考

Simple Password Authentication-Sorcery