This time, we will learn how before_action
works through application development.
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.
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.
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