エンジニアとしての市場価値を測りませんか?PR

企業からあなたに合ったオリジナルのスカウトを受け取って、市場価値を測りましょう

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 1 year has passed since last update.

【Rails】フロントから渡ってきたパラメータをバックエンド側で編集できない

Last updated at Posted at 2022-09-06

以下のような感じのバックエンド側に渡したストロングパラメータをgsubでちょこっと手を加えようとしたらできなかったので備忘録も兼ねてメモ。。。

def permit_params
 params.require(:form_params).permit(:param1,
                                     :param2,
                                     :param3)
end

挙動としては以下のような感じ、、、

permit_params[:param1].gsub("hoge","fuga") #←これをログ出力したらきちんとgsubが効いているが、、、
permit_params[:param1] = permit_params[:param1].gsub("hoge","fuga") #←こんな感じで値を更新しようとしても書き換えができない。エラーとかも出ない。。。

対応
ストロングパラメータを他の変数に一旦、移してそっちを編集する。

params = permit_params
params[:param1] = params[:param1].gsub("hoge","fuga")
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

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
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?