2

I can't seem to get the class on this select to work. The grouped collection works...but not the 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'} 

1 Answer 1

2

Try without html: key:

= 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. Your code is very hard to read, try to extract some parts into variables, like this:

- 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'
Sign up to request clarification or add additional context in comments.

1 Comment

It only worked when I did your refactoring, The first option didn't work. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.