我正在嘗試使用 content_tag rails 助手顯示一個徽章藥丸,根據它的布林值顯示“是”或“否”。
我目前將我的輔助方法寫成
def boolean_for(bool = false)
style = [true, 'true', 1, '1'].include?(bool) ? ['success', t('Yes')] : ['danger', t('No')]
content_tag(:span, 'Selectable', class: "badge badge-pill badge-%s"% style).html_safe
end
顯示這個

我的目標是將“可選”更改為是或否,但到目前為止,我所做的每一次嘗試都沒有成功。我假設我必須更改第二個引數,但不確定是什么。我嘗試了這個,它確實顯示是或否,但會導致我失去徽章藥丸的樣式。
def boolean_for(bool = false)
rv=false
rv=true if bool
return rv ? t('Yes') : t('No')
style = [true, 'true', 1, '1'].include?(bool) ? ['success', t('Yes')] : ['danger', t('No')]
content_tag(:span, rv, class: "badge badge-pill badge-%s"% style).html_safe
end
對此的任何幫助將不勝感激!
uj5u.com熱心網友回復:
這(或類似的東西)可以解決問題:
def boolean_for(bool = false)
style = ['danger', t('No')]
pill_text = 'No'
if [true, 'true', 1, '1'].include?(bool)
style = ['success', t('Yes')]
pill_text = 'Yes'
end
content_tag(:span, pill_text, class: "badge badge-pill badge-%s"% style).html_safe
end
或者,如果您想像使用樣式一樣使用您的語言環境檔案:
def boolean_for(bool = false)
style = ['danger', t('No')]
pill_text = t('No')
if [true, 'true', 1, '1'].include?(bool)
style = ['success', t('Yes')]
pill_text = t('Yes')
end
content_tag(:span, pill_text, class: "badge badge-pill badge-%s"% style).html_safe
end
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/482377.html
上一篇:導軌-更改另一個表的欄位-布林值
