collection_selectにclassをつける方法
よく、promptの部分を記述しろ!みたいなこと書かれていますが、selectedの場合が書いていなかったので、書いておきます。
そもそものソースコードを見てみます。
def collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {}) Tags::CollectionSelect.new(object, method, self, collection, value_method, text_method, options, html_options).render end
rails/form_options_helper.rb at a0dcc95cd80c5546579fdc50294fdfefc289f41b · rails/rails · GitHub
optionsの後に渡すと、html_optionsと認識されるようですね。
これが理由でpromptの後につけろ!みたいなことが言われるんですね。
ということで、下記の2パターンで書きました。
collection_select :player_id, nil, @players, :id, :full_name, selected: @player_id, class: "form__select__box form__width-100" => "<select name=\"player_id[]\" id=\"player_id_\"><option selected=\"selected\" value=\"60012\">ダルビッシュ有</option></select>" collection_select :player_id, nil, @players, :id, :full_name, { selected: @player_id }, class: "form__select__box form__width-100" => "<select class=\"form__select__box form__width-100\" name=\"player_id[]\" id=\"player_id_\"><option selected=\"selected\" value=\"60012\">ダルビッシュ有</option></select>"
原因がわからないのは、selected
を{}
で囲うと、後続が認識されるんですよね。
これが謎ではまりました。
slimの特徴なのか・・・?