What is before_action?

Introduction

This time, we will learn how before_action works through application development.

What is before_acton?

It is used when you want to perform processing before executing the action of the rails controller, or when you want to combine the processing of the same description.

The basic writing method is as follows

tests_controller.rb


   before_action :Method name you want to process

In this case, the specified method will be processed before executing each method defined in the same controller.

Alternatively, you can limit the action with only or except as in the resources method.

tests_controller.rb


before_action :Method name you want to process, only: [:Action 1,:Action 2]

In this case, the specified "method to be processed" is executed only before actions 1 and 2 are executed.

Actual usage example

categories_controller.rb


class CategoriesController < ApplicationController
  def index
    @categorys = Category.all
    @category = Category.new
  end

  def create
    @category = Category.new(category_params)
    if @category.save
      redirect_to categories_path
    else
      @categorys = Category.all
      render :index
    end
  end

  def edit
    @category = Category.find(params[:id])⇦ Processing is suffering
  end

  def update
    @category = Category.find(params[:id])⇦ Processing is suffering
    if @category.update(category_params)
      redirect_to categories_path
    else
      render :edit
    end
  end

  def search
    @categorys = Category.where(is_valid: true)
    @category = Category.find(params[:id])⇦ Processing is suffering
    @q = @category.notes.all.ransack(params[:q])
    @notes = @q.result(distinct: true)
    @title = @category.name
    render 'notes/index'
  end


  private

  def category_params
    params.require(:category).permit(:name, :is_valid)
  end
end

As you can see, the description is covered in edit, update and search.

So refactor the above code and

categories_controller.rb


class CategoriesController < ApplicationController
  before_action :set_category, only: [:edit, :update, :search]⇦ Add


  def index
    @categorys = Category.all
    @category = Category.new
  end

  def create
    @category = Category.new(category_params)
    if @category.save
      redirect_to categories_path
    else
      @categorys = Category.all
      render :index
    end
  end

  def edit
  end

  def update
    if @category.update(category_params)
      redirect_to categories_path
    else
      render :edit
    end
  end

  def search
    @categorys = Category.where(is_valid: true)
    @q = @category.notes.all.ransack(params[:q])
    @notes = @q.result(distinct: true)
    @title = @category.name
    render 'notes/index'
  end


  private

  def category_params
    params.require(:category).permit(:name, :is_valid)
  end

  def set_category
    @category = Category.find(params[:id])⇦ Summarizes common processing
  end
end

The common process is summarized by a method called set_category, and it is called by before_action when executing the update action, edit action, and search action.

authenticate_user!

before_action :authenticate_user!

The above has "authenticate_user!" which is a method that can be used when devise is introduced. This method transitions to the login screen if you are not logged in.

You can often see that you are using a mail-order site such as Amazon, but you can browse products without logging in, but you will be asked to log in when you proceed to purchase.

In this way, before_action specifies the common processing that you want to be performed before executing any action.

Finally

This time I somehow understood how before_action works. I'm glad if you can use it as a reference. I will update it as needed if I find out anything else.

Recommended Posts

What is before_action?
What is Cubby
What is Docker?
What is null? ]
What is java
What is Keycloak
What is maven?
What is Docker
What is self
What is Jenkins
What is ArgumentMatcher?
What is IM-Juggling?
What is SLF4J?
What is Facade? ??
What is Java <>?
What is Gradle?
What is POJO
What is Java
What is RubyGem?
What is programming?
What is Docker
What is Byte?
What is Tomcat
What is Maven Assembly?
What is `docker-compose up`?
What is a constructor?
What is vue cli
What is an interface?
What is Ruby's self?
What is hard coding?
What is a stream
What is Ruby's attr_accessor?
What is Java Encapsulation?
What is permission denied?
What is instance control?
What is an initializer?
What is an operator?
What is object orientation?
What is Guava's @VisibleForTesting?
What is MVC model?
What is an annotation?
What is Java API-java
What is @ (instance variable)?
What is Gradle's Artifact?
Use before_action! !!
What is before_action?
What is JPA Auditing?
[Swift] What is dismiss?
[Java] What is flatMap?
What is a Servlet?
What is web development?
[Java] What is JavaBeans?
[Android] What is Context? ??
[Java] What is ArrayList?
[Ruby] What is true?
What is object-oriented after all?
What is HttpSession session = request.getSession ();
What is docker run -it?
What is Java Assertion? Summary.
[Memorandum] What is an error?
What is DI (Dependency Injection)
What is a wrapper class?