简体   繁体   中英

Inline string split to place comma separated values as CSS classes in View

I have a field in my table like:

tank, troublesome, athletic, powerback

That's a single string.

I'd like to comma separate these values and place them as CSS classes for an element. Here's my attempt:

<a href="<%= player_path(player) %>" class="player-item <% players.roles.split(",").each do |role| print role end %>">

But I get:

<a href="/players/1" class="player-item ">

Any suggestions?

You can just replace the commas with nothing:

<a href="<%= player_path(player) %>" class="player-item <% player.roles.gsub(',', '') %>">

But I think there is a better solution that would involve refactoring your database. Having fields of comma delimited values is almost never a good idea :) but that is a different story.

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