在 Rails 4 形式中将 class 添加到 grouped_options_for_select

adding class to grouped_options_for_select in Rails 4 form

我似乎无法让 class 在这个 select 上工作。分组集合有效......但不是 class:

 = f.select :topic_id, grouped_options_for_select([['News',  @topics.news.order(title: :asc).collect {|v| [ v.title, v.id ] }],
        ['Opinion',    @topics.opinion.order(title: :asc).collect {|v| [ v.title, v.id ] }]]), html: {include_blank:  false , id:  'page_topic', class:  'form-control'} 

不用 html: 键试试:

= f.select :topic_id, grouped_options_for_select([['News',  @topics.news.order(title: :asc).collect {|v| [ v.title, v.id ] }],
    ['Opinion', @topics.opinion.order(title: :asc).collect {|v| [ v.title, v.id ] }]]), include_blank:  false, id: 'page_topic', class:  'form-control'

P.S。您的代码很难阅读,请尝试将某些部分提取到变量中,如下所示:

- news_options = ['News', @topics.news.order(title: :asc).collect {|v| [ v.title, v.id ] } ]
- opinion_options = ['Opinion', @topics.opinion.order(title: :asc).collect {|v| [ v.title, v.id ] }]    
- options = grouped_options_for_select([news_options, opinion_options])
= f.select :topic_id, options, include_blank: false, id: 'page_topic', class: 'form-control'