[RUBY] Use before_action! !!

What is before_action?

Here I will briefly explain what before_action is.

before_action is defined in the controller when using before_action A method that allows common processing to be performed before an action is executed. In other words, it is used when you want to process before executing the rails controller action, or when you want to combine the processes with the same description. </ font>

Even if it is explained in words, "Doyukoto?", "I can't imagine ..." I think so I would like to explain with an example below.

before_action description example

[Example] before_action


class controller name< ApplicationController
  before_action :Method name you want to process

We also use the options here at the same time. You don't have to use before_action for every action, so Specify which action you want to process before executing. The option used there is the only option.

What is the only option? (Description example)

By using the only option as with resources, it is possible to limit which action to execute before executing it. </ font>

"resources" </ font> is a method that allows you to set the routing of 7 actions at once.

[Example] before_action


class controller name< ApplicationController
  before_action :Method name you want to process, only: [:Action name, :Action name]

Use the only option after before_action as described above. You can specify the action by writing as in the example.

The basics are described as above.

How should I write even if I refer to this example? I think it will be The following is an explanation of the overall description of the controller.

Controller whole description example

Here, I would like to explain by giving an example of the entire description of the controller.

This is a description example when before_action is not used first. I would like you to see the example below, but there is the same description.

When not in use

[Example] 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])⬅️ Here! !!
  end

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

  def show
    @tweet = Tweet.find(params[:id])⬅️ Here! !!
  end

  private

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

Looking at the edit and show actions in the tweets controller, @tweet = Tweet.find (params [: id]) </ font> is repeatedly described. I would like to summarize the description of this part.

In the above example, there is another description that is covered, This time, I will use before_action in only one place, so I will summarize it. Do not mind. w

The following is a description example when used.

When using

Write the description by executing "only" processing of edit action and show action I would like to summarize it. This time, let's call the method before_action test_tweet!

[Example] 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

By using before_action, you can use the edit and show actions Before execution, we call a method called test_tweet.

By doing so, it is a common process of the two actions. @tweet = Tweet.find (params [: id]) works.

Repeat by using before_action as in the above example There is nothing to describe, so it's refreshing and easy to see! !!

Summary

· before_action is before the action defined in the controller is executed. A method that can perform specified common processing </ font> -The only option uses the only option like resources to limit which action to execute before executing it. </ Font>

!! !! When writing code from these things, it is good to write it in an easy-to-read manner without repeating the same description! !!