1. before_cation とは
before_cation
とは、何かのアクションをする前に実行することができるメソッド。例えば、ユーザーがログインしているかどうかを、各ページに移動するごとに確認するときに使用する。
2. before_cationの簡単な例
・各ページのコントローラーが実行される前に、コントローラ全体を司っているコントローラのbefore_action :get_category
が呼ばれる。
/app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
# 各ページのコントローラーが実行される前に、
# ↓↓↓(get_test)が呼ばれる。
before_action :get_test
def get_test
@tests = Test.all
end
end