5
5

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 5 years have passed since last update.

params.requireとparams.permitの違い

Posted at
class UsersController < ApplicationController
  def show
    @user = User.find(params[:id])
  end
 
  def edit
  end
 
  def update
    current_user.update(update_params)
  end
 
  private
  def update_params
    params.require(:user).permit(:family_name, :first_name, :family_name_kana, :first_name_kana)
  end
end

##params.requireとparams.permitの違いとは

入れ子構造になっている。例えばform tagを使った時userに対して情報がハッシュで追加されることになるが、それはuserの数が増えるほど増えていく。つまり入れ子構造になっているのである。親のハッシュにはrequireを、その下のハッシュにはpermitを使うと覚えるといいだろう。

5
5
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
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?