LoginSignup
0
0

More than 1 year has passed since last update.

requireとは?permitとは?ストロングパラメータとは?

Posted at

はじめに

WebからPostリクエストやPatchリクエストによって送られきた情報はparamsの中のモデルの中の値のように入れ子構造になって格納され、サーバーに渡される。

app/controllers/posts_controller.rb
  def post_params
    # params[:post][:content]のように階層構造になっている
    params.require(:post).permit(:content)
  end

これだけのコードだが、一つずつ読み解いていく。

使用環境は以下の通りである。

No 項目 内容
1 OS Mac
2 Ruby 2.6.3
3 rails 6.0.4

目次

1.requireとは
2.permitとは
3.ストロングパラメータとは
4.まとめ

1. requireとは

params.require(:post) で、paramsに渡すモデルを指定する。

2. permitとは

params.permit(:content) で、paramsに渡すキーを指定する。
ちなみに、これらの指定はストロングパラメータと呼ばれる。
参考元

3. ストロングパラメータとは

本当に必要な情報のみをWeb上でやり取りすることができるように設定すること。
これにより、admin権限の改ざんなど、悪質な行為を制御できる。

4. まとめ

params.require(:post).permit(:content) で、paramsに渡すモデルを:post、キーを:contentに指定する。
これにより、params[:post][:content]に値が渡される。

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