2013-10-03 24 views
-1

我試圖做的是打印Pending Quotes如果計數爲0>1Pending Quote如果算上==1但如果count>1,輸出爲2 true,在另外兩起案件做工精細,但並我看不到任何明顯的東西。紅寶石三元如果錯誤

<%= @pending.nil? ? '0' : @pending.count %> 
<%= ([email protected]? and @pending.count > 1) or ([email protected]? and @pending.count == 0) ? 'Pending Quotes' : 'Pending Quote' %> 

回答

1

你必須寫這種方式:

(([email protected]? and @pending.count > 1) or ([email protected]? and @pending.count == 0)) ? 'Pending Quotes' : 'Pending Quote' 

你也可以把它寫成

([email protected]? && (@pending.count > 1 || @pending.count == 0)) ? 'Pending Quotes' : 'Pending Quote' 
+0

啊,現在感覺很傻。謝謝,當它允許我時,將其標記爲接受:) – martincarlin87

+2

注意:在Ruby中習慣於使用'&&''||'布爾表達式。使用'和''或'進行流量控制。另外,OP可能值得注意的是「!x.nil?」通常比「x」更好。 – tokland

+0

@tokland我同意你 –

5

我會用pluralize幫手:

<%= pluralize(@pending, 'Pending Quote') %> 
+0

我不知道有點ROR ..所以我堅持只有Ruby .. :) –

1

隨着De Morgan的幫助...

@pending.try(:count) == 1 ? 'Pending Quote' : 'Pending Quotes' 
+0

謝謝,這是一個非常乾淨的方式來把它 – martincarlin87