我的问题是:这个帮助器将在视图中使用,并且还会向' li'添加类。元素,除了类' red',如果满足以下条件,则应添加该元素。有没有人看到我需要改变的东西来使这项工作?
def navigation_item_with_message(body, url, options = {})
count = options.delete(:count) || 0
body = "#{body} (#{count})"
li_classes = ['ribbon', 'ribbon-inprogress']
li_classes << options[:li_class] = 'red' if count > 0 navigation_item(body, url, options)
li_classes.join(' ')
end
答案 0 :(得分:0)
我不确定您提供的代码段是什么,因为我看不到navigation_item
的实现。然而,这是一种做你想做的事情的方法:
li_classes = ['ribbon', 'ribbon-inprogress']
li_classes << 'red' if count > 0
li_classes.join(' ')
# You can also do
# li_classes = "ribbon ribbon-inprogress #{count > 0 ? 'red' : ''}"
content_tag(:li, class: li_classes) do
# function that returns an HTML or plain string
end
答案 1 :(得分:0)
所以我想出了如何解决它。
def navigation_item_with_message(body, url, options = {})
count = options.delete(:count) || 0
body = "#{body} (#{count})"
li_classes = [options[:li_class]]
li_classes << 'red' if count > 0
options.merge!(:li_class => li_classes.join(' '))
navigation_item(body, url, options)
end