2013-07-24 45 views
0

我現在有這個代碼。如何在一行中編寫「If case with case」?

<div class="alert alert-<%= name == :notice ? "success" : "error" %>"> 

但是,在這些旁邊還有「info」和「block」,比如「success」和錯誤。

不幸的是,當'name ==:notice'時,它必須是「成功」。 但一切纔可以,因爲它就像

when `name == :error`, it has to be "error" 
when `name == :info`, it has to be "info" 
when `name == :block`, it has to be "block" 

我怎麼能在一條線上假定這4個選項的代碼呢?

回答

1

如果只有一個例外,那怎麼樣:

<div class="alert alert-<%= name == :notice ? 'success' : name.to_s %>"> 
+0

由於這是它! – MKK

+0

我寧願去這裏幫助。 –

+0

@MichaelSzyndel多次使用助手會使應用程序變慢,不是嗎? – MKK