2017-09-13 56 views
1

我必須顯示可以評分的國家列表。如果一個國家已經被評爲那麼用戶不能「unrate」它在選擇的條件中包含空白選項

# countries index 
= render @countries 

# countries/_country 
= form_for country do |f| 
    = f.select :rating, 
    Rating.options, 
    include_blank: "False if country is rated. True otherwise" 

我試圖通過一個進程來include_blank選項,但它沒有工作

我可以使用助手來計算它看起來像選項這個:

def rating_options(country) 
    if country.rating.present? 
    Rating.options 
    else 
    [""] + Rating.options 
    end 
end 

有沒有更好的方法來在條件選擇標籤中包含空白選項?

回答

1

你可以傳遞到include_blank選項的任何輔助方法或條件

= form_for country do |f| 
    = f.select :rating, 
    Rating.options, 
    include_blank: f.object.rating.empty? 
+0

謝謝你,該解決方案是如此簡單 - 我可以計算include_blank在線!我甚至可以使用國家局部變量 - 'include_blank:country.rating.blank?'出於某種原因,我認爲我需要lambda或helper – Hirurg103