可能重複:
Checking if a variable is defined in Ruby呼應,如果變量在紅寶石/ ERB定義
使用傾斜的模板render
方法,我通過在
#... t setup ...
t.render(self, { :a => 'test', :b => 'again' })
在我template.erb
<%= a %>
<%= b %>
說我從傳遞給模板的散列中刪除:b
。渲染將失敗,因爲:b
未定義。
在PHP中,我可以去:
<?= isset($foo) ? $foo : '' ?>
有沒有乾淨的方式(以紅寶石/ ERB),以 「回聲如果」?
我想<%= b.nil? ? b : '' %>
但這顯然是錯誤的。任何幫助將不勝感激
這會有幫助嗎? http://stackoverflow.com/questions/288715/checking-if-a-variable-is-defined-in-ruby –
有幫助,但我想我的問題是特定於erb模板約定 – tester