9
5

More than 5 years have passed since last update.

Rails hidden_fieldの渡し方、受けとり方

Posted at

渡し方

値を指定する場合はvalueを使う。
第一引数にオブジェクト名、第二引数部分にvalueで受け渡す値を指定する。

text.html.erb
<%= f.hidden_field :unique_key, :value => params[:unique_key] %>

受け取り方

valueで渡した値をモデルのインスタンスの属性に格納している場合、以下のようになる。

text_controller.rb

@introduce = Follow.find_by(unique_key: params[:follow][:unique_key])

Followモデルのunique_keyカラムの値を受け取る場合。

参考

Ruby on Rails - hidden_fieldの使い方
https://qiita.com/azusanakano/items/5849a7ca74be58d195b7
f.hidden_fieldとhidden_field_tagの使い方【Ruby on Rails】
https://sakurawi.hateblo.jp/entry/hidden_field

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