13

I've looked at How do I set the HTML options for collection_select in Rails? and I'm sure I'm missing something obvious, but I can't get this to work.

My select currently looks like:

  <%= f.collection_select :broadcast_id, broadcasts, :id, :to_s,
    :include_blank => 'Broadcast on...' %>

and I've tried including :class => 'prevent_collapse', which does nothing, as well as {:class => 'prevent_collapse'}, which gives me an error.

If anyone can point out how to do this, I'll be super grateful!

Community
  • 1
  • 1
tiswas
  • 2,041
  • 7
  • 31
  • 43

2 Answers2

22
collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
=>
f.collection_select :broadcast_id, broadcasts, :id, :to_s,
{:include_blank => 'Broadcast on...'}, {:class => 'prevent_collapse'}

And what error do you have?

And does broadcast item has got :to_s method? It will return class name, as I think.

fl00r
  • 82,987
  • 33
  • 217
  • 237
  • 2
    thank you - that's solved it! I was forgetting to put the :include_blank in {} – tiswas Feb 09 '11 at 13:33
  • you can leave like that: "f.collection_select :broadcast_id, broadcasts, :id, :to_s, {}, {:class => 'prevent_collapse'}" There is no need to add "include_blank" if you do not need it. – Bruno Paulino Jan 10 '16 at 22:25
1

Is that field :include_blank => {}, compulsory ? I tried with :include_blank => false and it worked. I wonder if we can avoid it ?

Shyamkkhadka
  • 1,438
  • 4
  • 19
  • 29
  • 1
    Yes you can skip it. Just replace `{:include_blank => 'Broadcast on...'}` with `{}`. – W.M. Feb 17 '17 at 16:58