显示是/否带有布尔值的content_tag助手

发布于 2025-02-01 12:52:01 字数 834 浏览 3 评论 0原文

我正在尝试使用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

select tags

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巷雨优美回忆 2025-02-08 12:52:01

这个(或类似的东西)会解决问题:

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 

This (or something like it) will do the trick:

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 

Or if you want to use your locale file like you are for the styles:

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 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文