[RAILS] [Ruby] It's a convenient guy ~ before_action ~

It is such a wonderful command "before_action" edition that summarizes what you are wearing in the description in the controller.

class ItemsController < ApplicationController

  def index
    @items = Item.order('created_at DESC')
  end

  def new
    @item = Item.new
  end

  def create
    @item = Item.new(item_params)
    if @item.save
      redirect_to root_path
    else
      render :new
    end
  end

  def show
    @item = Item.find(params[:id])
  end

  def edit
    @item = Item.find(params[:id])
    redirect_to root_path unless current_user.id == @item.user_id
  end

  def update
    @item = Item.find(params[:id])
    @item.update(item_params)
    if @item.save
      redirect_to root_path
    else
      render :edit
    end
  end

end

There is a description that is worn in the action.

show
edit
update

in order from the top The above three descriptions are "** @ item = Item.new (item_params) **".

Let's summarize this using before_action.

class ItemsController < ApplicationController
before_action :find_item ⬅️ ❶ First, describe this

~ Abbreviation ~

  def show
    @item = Item.find(params[:id])⬅️❷ Erase this
  end

  def edit
    @item = Item.find(params[:id])⬅️❷ Erase this
    redirect_to root_path unless current_user.id == @item.user_id
  end

  def update
    @item = Item.find(params[:id])⬅️❷ Erase this
    @item.update(item_params)
    if @item.save
      redirect_to root_path
    else
      render :edit
    end
  end

private ⬅️❸ Make it a private method

 
  def find_item                 ⬅️❹find_Define a method named item and IN the description that the contents are wearing
    @item = Item.find(params[:id])
  end


And page update!

Yes! I got an error! Lol The defined find_item method is only needed for actions that are worn and stripped of description. In other words, I want to apply it only to ** show **, ** edit **, and ** update **. At that time, it was only action. Let's squeeze.

class ItemsController < ApplicationController
before_action :find_item, only: [:show, :edit, :update]⬅️ ❶ Add here
~ Abbreviation ~

  def show
  end

  def edit
    redirect_to root_path unless current_user.id == @item.user_id
  end

  def update
    @item.update(item_params)
    if @item.save
      redirect_to root_path
    else
      render :edit
    end
  end

  private
 
  def find_ite
    @item = Item.find(params[:id])
  end

It was pretty refreshing. I wondered if this kind of thing would happen easily if I wrote it in the same file all the time. It is important to organize and check once the code increases. It was a good experience today as well.

Recommended Posts

[Ruby] It's a convenient guy ~ before_action ~
It's a proxy festival ☆ Wasshoi!
Ruby Learning # 2 Drawing a Shape
[Ruby] Extracting a two-dimensional array
What is a Ruby module?
Creating a calendar using Ruby
Multiplication in a Ruby array
Create a fortune using Ruby
Ruby Learning # 32 Building a Quiz
Ruby ① Build a Windows environment
Cut out a Ruby string