74
61

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.

f.selectで生成されるoptionタグを改変する

Last updated at Posted at 2019-07-09

#やりたいこと
"f.select"で生成されるセレクトボックスの中身(optionタグ)をいじりたい
初期値を設定したい

#概要

.rb
= f.select :name, options_for_select(セレクトボックスで表示したい要素配列.map{|c|[セレクトボックスで表示される値, value属性値にセットしたい値, {追加属性記述}]}, 初期値のvalueを選択), {}, {class: 'hoge', id: 'hogehoge'}

value属性値にセットしたい値の記述をなくすと
"value = セレクトボックスで表示される値"
になります

#具体例

.rb
#セレクトボックスの選択肢にしたい要素配列
@sample_array_1 = ['aaa', 'bbb', 'ccc']
@sample_array_2 = [{id: 1, name: "aaa"}, {id: 2, name: "bbb"}, {id: 3, name: "ccc"}]
#初期値要素(データベースから読み取ったと仮定)
@selected_sample_1 = 'bbb'
@selected_sample_2 = {id: 2, name: "bbb"}

#基本形1
= f.select :sample, @sample_array_1, {}, {class: 'hoge', id: 'hogehoge'}
#基本形2
= f.select :sample, options_for_select(@sample_array_2.map{|c|[c[:name], c[:id]]}), {}, {class: 'hoge', id: 'hogehoge'}
#属性追加1
= f.select :sample, options_for_select(@sample_array_1.map{|c|[c, {'data-sample'=>c}]}), {}, {class: 'hoge', id: 'hogehoge'}
#属性追加2
= f.select :sample, options_for_select(@sample_array_2.map{|c|[c[:name], c[:id], {'data-sample'=>c[:id]}]}), {}, {class: 'hoge', id: 'hogehoge'}
#初期値設定1
= f.select :sample, options_for_select(@sample_array_1.map{|c|[c, {'data-sample'=>c}]}, @selected_sample_1), {}, {class: 'hoge', id: 'hogehoge'}
#初期値設定2
= f.select :sample, options_for_select(@sample_array_2.map{|c|[c[:name], c[:id], {'data-sample'=>c[:id]}]}, @selected_sample_2.id), {}, {class: 'hoge', id: 'hogehoge'}

###基本形1

.rb
= f.select :sample, @sample_array_1, {}, {class: 'hoge', id: 'hogehoge'}
.html
#基本形1
<select class="hoge" id="hogehoge" name="sample">
  <option value="aaa">aaa</option>
  <option value="bbb">bbb</option>
  <option value="ccc">ccc</option>
</select>

###基本形2

.rb
= f.select :sample, options_for_select(@sample_array_2.map{|c|[c[:name], c[:id]]}), {}, {class: 'hoge', id: 'hogehoge'}
.html
#基本形2
<select class="hoge" id="hogehoge" name="sample">
  <option value="1">aaa</option>
  <option value="2">bbb</option>
  <option value="3">ccc</option>
</select>

###属性追加1

.rb
= f.select :sample, options_for_select(@sample_array_1.map{|c|[c, {'data-sample'=>c}]}), {}, {class: 'hoge', id: 'hogehoge'}
.html
#属性追加1
<select class="hoge" id="hogehoge" name="sample">
  <option data-sample="aaa" value="aaa">aaa</option>
  <option data-sample="bbb" value="bbb">bbb</option>
  <option data-sample="ccc" value="ccc">ccc</option>
</select>

###属性追加2

.rb
= f.select :sample, options_for_select(@sample_array_2.map{|c|[c[:name], c[:id], {'data-sample'=>c[:id]}]}), {}, {class: 'hoge', id: 'hogehoge'}
.html
#属性追加2
<select class="hoge" id="hogehoge" name="sample">
  <option data-sample="1" value="1">aaa</option>
  <option data-sample="1" value="1">bbb</option>
  <option data-sample="1" value="1">ccc</option>
</select>

###初期値設定1

.rb
= f.select :sample, options_for_select(@sample_array_1.map{|c|[c, {'data-sample'=>c}]}, @selected_sample_1), {}, {class: 'hoge', id: 'hogehoge'}
.html
#初期値設定1
<select class="hoge" id="hogehoge" name="sample">
  <option data-sample="aaa" value="aaa">aaa</option>
  <option selected="selected" data-sample="bbb" value="bbb">bbb</option>
  <option data-sample="ccc" value="ccc">ccc</option>
</select>

###初期値設定2

.rb
= f.select :sample, options_for_select(@sample_array_2.map{|c|[c[:name], c[:id], {'data-sample'=>c[:id]}]}, @selected_sample_2.id), {}, {class: 'hoge', id: 'hogehoge'}
.html
#初期値設定2
<select class="hoge" id="hogehoge" name="sample">
  <option data-sample="1" value="1">aaa</option>
  <option selected="selected" data-sample="1" value="1">bbb</option>
  <option data-sample="1" value="1">ccc</option>
</select>

#参考
https://stackoverflow.com/questions/18100834/how-to-make-the-f-select-rails-selected
https://stackoverflow.com/questions/5052889/ruby-on-rails-f-select-options-with-custom-attributes

74
61
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
74
61

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?