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 3 years have passed since last update.

before_action使うべし!!

Posted at

before_actionってなに?

ここでbefore_actionがなんなのか簡単に説明させて頂きます。

before_actionとは、before_actionを使用するとcontrollerで定義された
アクションが実行される前に、共通の処理を行うことができるメソッドです。
つまり、railsのcontrollerアクションを実行する前に処理を行いたい時、同じ記述の処理をまとめたい時に使用します。

言葉で説明されても「どゆこと?」、「イメージができない。。。」と
思いますので下記に例を上げて説明したいと思います。

before_action記述例

[例] before_action
class コントローラ名 < ApplicationController
  before_action :処理させたいメソッド名

また、ここではオプションを同時に使用します。
全部のアクションにbefore_actionを使用する必要はないので、
どのアクションで実行する前に処理を行わせたいのか指定します。
そこで使用するオプションがonlyオプションです。

onlyオプションってなに?(記述例)

resourcesと同様にonlyオプションを使用することによって、どのアクションの実行前に、処理を実行させるかなど制限するとこが可能です。

『resources』とは、7つのアクションのルーティングをまとめて設定ができるメソッドのことです。

[例] before_action
class コントローラ名 < ApplicationController
  before_action :処理させたいメソッド名, only: [:アクション名, :アクション名]

上記のようにbefore_actionの後ろにonlyオプションを記述して使用します。
例のように記述することでアクションを指定することができます。

基本は上記のように記述します。

この例を参照してもどう書いたらいいの?となると思いますので、
次はcontrollerの全体的な記述例を上げて説明します。

controller全体記述例

ここでは、controllerの全体の記述例を上げて説明していきたいと思います。

先にbefore_actionを使用していない時の記述例です。
下記の例を見て頂きたいのですが、同じ記述がありますね。

使用しない場合

[例] controller
class TweetsController < ApplicationController

  def index
    @tweets = Tweet.all
  end

  def new
    @tweet = Tweet.new
  end

  def create
    Tweet.create(tweet_params)
  end

  def destroy
    tweet = Tweet.find(params[:id])
    tweet.destroy
  end

  def edit
    @tweet = Tweet.find(params[:id])    ⬅️ ここの箇所!!
  end

  def update
    tweet = Tweet.find(params[:id])
    tweet.update(tweet_params)
  end

  def show
    @tweet = Tweet.find(params[:id])    ⬅️ ここの箇所!!
  end

  private

  def tweet_params
    params.require(:tweet).permit(:name, :image, :text)
  end
end

tweetsコントローラーのeditアクションとshowアクションを見ると、
@tweet = Tweet.find(params[:id])が繰り返し記述されています。
この箇所の記述をまとめていきたいと思います。

上記の例だともうひとつ被っている記述がありますが、
今回は一箇所だけbefore_actionを使用してまとめていきますので、
気にしないで下さい。w

次は使用した時の記述例です。

使用する場合

editアクションとshowアクションの処理"のみ"を実行させて記述を
まとめていきたいと思います。
今回は、before_actionで呼び出すメソッドをtest_tweetとしましょう!

[例] controller
class TweetsController < ApplicationController
  before_action :set_tweet, only: [:edit, :show]

  def index
    @tweets = Tweet.all
  end

  def new
    @tweet = Tweet.new
  end

  def create
    Tweet.create(tweet_params)
  end

  def destroy
    tweet = Tweet.find(params[:id])
    tweet.destroy
  end

  def edit
  end

  def update
    tweet = Tweet.find(params[:id])
    tweet.update(tweet_params)
  end

  def show
  end

  private

  def tweet_params
    params.require(:tweet).permit(:name, :image, :text)
  end

  def test_tweet
    @tweet = Tweet.find(params[:id])
  end
end

before_actionを使用することで、editアクションとshowアクションの
実行前に、test_tweetというメソッドを呼び出しています。

そうすることで、2つのアクションの共通の処理である
@tweet = Tweet.find(params[:id])が動きます。

上記の例のようにbefore_actionを使用してまとめてあげることで繰り返し
記述することがないのでスッキリして見やすくなりましたね!!

まとめ

・before_actionは、コントローラで定義されたアクションが実行される前に、
指定した共通の処理を行うことができるメソッドのこと

・onlyオプションは、resourcesと同様にonlyオプションを使用することによって、どのアクションの実行前に、処理を実行させるかなど制限すること

!!これらの事からコードを記述する際は、同じ記述を繰り返さずに見やすく記述していくのがいいですね!!

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?