LoginSignup
1
0

More than 3 years have passed since last update.

コントローラ内でどのアクションに対しても一定の前処理をする

Last updated at Posted at 2019-09-03

コントローラ内でどのアクションに対しても一定の前処理をする

アクション・コールバック

「ログインしているユーザーにだけ特定のコンテンツを表示したい」
みたいな時に使えるのがアクション・コールバック。

【要点】

コントローラ1で定義したアクションを、コントローラ2のはじめに呼び出す。

sample1_controller.rb
class Sample1Controller < ApplicationController

  before_action :do_something

  private def do_something
     #アクションが実行される前に行う処理
  end

end
sample2_controller.rb
class Sample2Controller < ApplicationController

  before_action :do_something

  def index
     #省略
  end      

end

(随時更新 2019/09/14)

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