显示是/否带有布尔值的content_tag助手
我正在尝试使用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
对此的任何帮助将不胜感激!
I am trying to display a badge pill that says 'Yes' or 'No' based on it's boolean value, using a content_tag rails helper.
I currently have my helper method written out as
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
which displays this
My goal is to change the "Selectable" to yes or no, but so far every attempt I have made has been unsuccessful. I'd assume I have to change the second argument, but am unsure to what.
I attempted this which did display yes or no, but would cause me to lose the badge pill styling.
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
Any help with this would be super appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个(或类似的东西)会解决问题:
或者如果您想像样式一样使用本地文件:
This (or something like it) will do the trick:
Or if you want to use your locale file like you are for the styles: