2012-10-05 44 views
0

我有這樣的任務:當用戶編輯他的對象 - 列表中的第一個選項將是對象的值。如何在選擇框中搜索並刪除使用Rails的重複選項?

<%= f.select :category, options_for_select([ 
     "#{@website.category}", 
     "Banking", 
     "Computers", 
     "Coupons", 
     "Directory", 
     "Fashion", 
     "Finance", 
     "Gifts", 
     "Maps", 
     "Media", 
     "Mobile", 
     "News", 
     "Tickets", 
     "Tech blog", 
     "Tech website", 
     "Trains", 
     "Travel", 
     "Transportation - general", 
     "Recipes", 
     "Another... "])%> 

任何人都可以建議如何做到這一點:

我使用這個代碼生成我的選擇列表?

回答

1

這裏看到該文檔,約options_for_select

您可以設置選擇框的默認值(或選擇的值)作爲第二個參數。 在你的情況下,它會像:

<%= f.select :category, options_for_select([ 
    "Banking", 
    "Computers", 
    "Coupons", 
    "Directory", 
    "Fashion", 
    "Finance", 
    "Gifts", 
    "Maps", 
    "Media", 
    "Mobile", 
    "News", 
    "Tickets", 
    "Tech blog", 
    "Tech website", 
    "Trains", 
    "Travel", 
    "Transportation - general", 
    "Recipes", 
    "Another... "], @website.category)%> 

希望這有助於!

+0

好吧,但它會重複列表中的類別 – MID

+0

編號閱讀文檔。 '@ website.category'返回的值必須與該選項的值相同。 – MrYoshiji

+0

對不起,我生氣了一點。我對新手有很大的麻煩) – MID

相關問題