2011-02-09 97 views
11

我看過How do I set the HTML options for collection_select in Rails?,我確定我錯過了一些顯而易見的事情,但我無法得到它的工作。將課程添加到collection_select

我選擇目前的樣子:

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

我嘗試了包括:類=> 'prevent_collapse',它什麼都不做,以及{:類=> 'prevent_collapse'},這給了我一個錯誤。

如果有人能指出如何做到這一點,我會非常感激!

回答

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'} 

你有什麼錯誤?

並且廣播項目已得到:to_s方法嗎?我認爲它會返回類名。

+2

謝謝 - 這就解決了!我忘記將:include_blank放在{} – tiswas 2011-02-09 13:33:19

+0

中,您可以這樣離開:「f.collection_select:broadcast_id,broadcasts,:id,:to_s, {},{:class =>'prevent_collapse'}」沒有如果你不需要它,需要添加「include_blank」。 – 2016-01-10 22:25:44

1

是否爲必填項:include_blank => {}?我嘗試了:include_blank => false,它工作。我想知道我們是否可以避免它?