Search code examples
htmlcssruby-on-railsacts-as-taggable-on

Rails adding background css to the matching results


controllers/users_controller.rb

@users = User.tagged_with(current_artist.tag_list, any: true)

index.html.erb

<% @users.each do |user| %>
     Tags: <%= user.tag_list.join(', ') %>
<% end %>

The above code displays all the users with one or more matching tags. How can I add CSS background colour to the tags that matches. I tried the following and it kind of works but it creates an extra tag which i don't want.

<span style="background-color:red;"><%= current_user.tag_list & user.tag_list %></span>

Solution

  • Try this:

    <%= user.tag_list.map{|t| current_artist.tag_list.include?(t) ? "<span class='tag-cl'>#{t}</span>" : "#{t}" }.join(',').html_safe %>