LoginSignup
20
18

More than 5 years have passed since last update.

Railsのf.selectにclassが設定できなかった

Posted at

現象

Rubyもあやしい僕ですが、最近Railsを触ってます。

そんな中でf.selectを使用してclass属性を設定しようとしたところ何故か設定できませんでした。

<%= f.select :object, :property, :class => 'foobar' %>
<select id="foo_object">
  :

日本語のRailsドキュメントを読んでも問題なさそうだし、何がいけないんだろう…。

修正

ググッてみたところ、以下のようにしろということでした。

# f.select 第3引数に {} を追加
<%= f.select :object, :property, {}, :class => 'foobar' %>

すると

<select id="foo_object" class="foobar">
  :

ちゃんと出力された!!
{}が気持ち悪ーい!!

原因

原因は先ほどのRailsドキュメントのソースコードへのリンクを見ればわかりました。

メソッドの定義が以下のようになっていました。

def select(method, choices = nil, options = {}, html_options = {}, &block)
  :

つまり、optionsをスキップして、html_optionsに設定する必要があったみたいです。なんだこれ…。

20
18
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
20
18