29
23

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

SimpleFormでの入力項目のデフォルト値設定方法

Last updated at Posted at 2017-02-09

SimpleFormを使っていて、入力項目のデフォルト値を設定したい時がありますが、「あれ?チェックボックスのデフォルトってどうやって設定するんだっけ?」とか毎回調べたりしてます。

毎回調べるのも面倒なので、まとめておきます。

バージョン: simple_form (3.3.1)

テキストボックスの場合

f.input :field_name, as: :text, input_html: { value: 'デフォルト値' }

テキストエリアの場合

テキストボックスの場合と同じですね。

f.input :field_name, as: :string, input_html: { value: 'デフォルト値' }

セレクトボックスの場合

青:blueを選択した状態にします。

f.input :field_name, as: :select, collection: [ ['赤', 'red'],[ '青', 'blue'], [ '黃', 'yellow'] ],
    , selected: 'blue'

ラジオボタンの場合

青:blueを選択した状態にします。

f.input :field_name, as: :radio_buttons, collection: [ ['赤', 'red'],[ '青', 'blue'], [ '黃', 'yellow'] ],
    , checked: 'blue'

チェックボックスの場合

ラジオボタンの場合と同じですね。
青:blueを選択した状態にします。
複数選択した状態にしたい場合は、配列にしておけば良いです。

f.input :field_name, as: :check_boxes, collection: [ ['赤', 'red'],[ '青', 'blue'], [ '黃', 'yellow'] ],
    , checked: 'blue'
29
23
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
29
23

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?