0
0

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.

自分用メモ(ログイン・findとfind_byの違い・helperって何、使い方は?)

Last updated at Posted at 2019-10-11

ログイン機構

ログインはセッションと言うリソースを作ることで実現する

sessionはcookieに保存する

ログインページへ(new)

情報入力
ログイン情報送信

セッションにユーザーIDが渡される

ログイン完了(ユーザーIDをセッションの中から取り出せる状態)

その後は
<%= @current_user.name %>
とかで取り出せるように

@current_userの有無・userによってデータ出力を変化させることができる

ログイン情報入力のフォームを作る

フォームはデータベースをいじるためのもの(なので基本モデルがある)(form_for(model) do |f|)

ユーザー登録との違いは、セッションにはセッションモデルが存在しないと言うこと
@userと言うようなインスタンス変数は存在しない

自分で追加情報を明示しないといけない(今回は変化を与えるリソースの名前と、対応するURL)

<%= form_for(:session,url: login_path) %>
f.email_form :e-mail:

送信されるデータは"session[:password]"
つまりparams[:session][:password]で受け取る

ログイン機能を作る

入力したsessionをparamsで受け取りemailを入力した[:email]から探す ↓ 見つかったらuserに代入する ↓ userに入っているpasswordと ↓ セッション入力されたpasswordが一致するかauthenticateでチェックする   ↓ (一致する?) ○ × →render new ↓  session[:user_id]=user.id

セッションの[:user_id]にuser.idを代入する

session[:user_id]がnilなら「誰もログインしていない状態」
session[:user_id]に値が代入されているなら、そのIDを持つユーザーがログイン中

ログイン者取得の簡略化

ログインしているかいないか、しているなら誰かを確認の確認が今後必要になってくる。 これはsession[:user_id]で取得すれば良い。 何回も呼び出すのが面倒なので、メソッドで定義しておく。

def current_user
@current_user =User.find_by(id: session[:user_id])
end
(findの場合、ユーザー存在しない場合エラーが発生してしまうので、今回はfind_byを使う)
(@current_userに代入することで、データベースを検索する回数を初めの一回だけにする。)

↓定義する前に、、代入演算子を理解しておく「||=」

「||=」と言う代入演算子

a ||= xxx

aが偽か未定義の場合、aに xxx を代入する。

これを書き換えると、
a = a || xxx
これはつまり、aに値が入っている場合、aにaは代入されるのでそのままである。
つまり、aに値が入っていない(nil)、もしくはfalseの時だけaが代入される。

続き

現在ログイン中のユーザーを取り出す(いる場合) def current_user if session[:user_id] @current_user = @current_user || User.find_by(id: session[:user_id]) end end

ログインしているユーザーが存在する場合(session[:user_id]がnilでない場合)で、
@current_userがnilもしくはfalseの場合、@current_userにsession[:user_id]を代入する。
(@current_userが存在する場合、そのまま)

「||=」を使って書き換え ↓
def current_user
if session[:user_id]
@current_user ||= User.find_by(id: session[:user_id])
end
end

findとfind_byの使い分け

find
→idのみ(idがわかっている時のみ使用可能)
idがわからない場合は使えない

find_by
→idじゃなくても良い(帰ってくる結果は一つだけ)
idがわからない場合(nilになる場合)こちらを使う

idがわかっているときはfind
idがわからないときはfind_byを使う

helperって何

Viewをシンプルに書くためのモジュール。 例としては form_for、link_to

基本的に、Viewでヘルパーメソッドを呼び出す

・Controllerで呼び出したい場合
→そのままでは呼び出せないので、application.controllerに一文加える必要がある

class ApplicationController < ActionController::Base
include SessionsHelper ←← これ
end

・helperを作るには?
アプリ全体でhelperを使いたい場合
→app/helpers/application_helper.rb に定義する
特定のモデルでhelperを使いたい場合
→app/helpers/(特定のモデル名)_helper.rb に定義する

・具体的にどうやって書くの?(セッションモデルにのみ適用の場合)(今回はcontrollerで使用する)

module SessionsHelper
(ここに書く)
end
(中身)
def log_in(user)
session[:user_id]=user.id
end

呼び出したいときは対象のコントローラーにメソッドを書くだけでok

pathにユーザー渡すのよくわからん

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?