requireとpermit
エラー問題めちゃくちゃ時間かかる。(2日かけて問題1 笑)
怪しい箇所の候補があまり浮かんでこない。
けど、数学の問題解いてるみたいで楽しい。
binding-pry
で裏側の動き見る感じ、もっと使いこなしたい。
もっとちょうだい。
form_with => params の中身
<ActionController::Parameters { "authenticity_token"=>"xxx", "post"=>{"title"=>"shige", "content"=>"tage"}, "commit"=>"投稿する", "controller"=>"posts", "action"=>"create" } permitted: false>
require(モデル名)
モデルに対応したキーがあるか確認。あればバリューを返す。
params.reqire(:post) => <ActionController::Parameters { "title"=>"shige", "content"=>"tage" } permitted: false>
require 補足
params[:post]でも同じ結果が得られる
params[:post] => <ActionController::Parameters { "title"=>"shige", "content"=>"tage" } permitted: false>
Railsの基本機能として、このように複数のキーとバリューのセットを一括処理することができる( == マスアサインメント機能)。ぱっと見便利。
が、例えばChormeの検証モードを使うとフォームの加工とか、送付されるparamsデータの加工は容易にできてしまう(らしい)。
permit(キー)
そこで登場するのがpermitメソッド。あらかじめ許可するキーを指定しておき、悪意ある書き込みを防止する。
params.require(:post).permit(:title, :content) => <ActionController::Parameters {"title"=>"shige", "content"=>"tage"} permitted: true>
└最後がpermitted: true
(許可)になってて腹落ち。
英単語
- syntax:syn-「一緒に」とtassein「整える」(tactics) -> 整理する => 文法的な構造、組み立て
用法:syntax error! == 構文のエラーだよ