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に書いて纏めると良い

def index
  @users = User.new 
end

def show
  @users = User.new 
end

def new
  @users = User.new 
end

こうなる

before_action :set_users

def set_users
  @users = User.new
end

各アクションが実行される前に呼ばれる

####特定のメソッドが呼ばれる前に呼びたい時

before_action :set_users, {only:[:edit, :update]}

onlyを使う時は1件でも複数件でも[]を使う

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?