LoginSignup
0
0

More than 3 years have passed since last update.

【RubyonRails入門】before_action

Last updated at Posted at 2019-09-10

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