简体   繁体   中英

Adding a class to <li> element with helper method ruby

my issue is: This helper will be used in the view and also add classes to the 'li' element, except class 'red', which should be added if the following condition is true. Does anybody see something I have to change to make this work?

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  

I'm no sure what the snippet you provided does, since I can't see the implementation of navigation_item . However here's a way to do what you want:

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

So I figured out how to solve it.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM