-4

So I have standart scaffold cycle that prints a string of tags of a meet.

<% @meets.each do |meet| %>
      <p class="MeetTags"><%= meet.tags %></p>
   <% end %>

How can I separate string of for example 3 words, so that each would be in separate box. Thank you!

  • 1
    If you have an answer that worked, you should accept it (click on the check mark next to the answer that worked best) – stevenspiel Nov 26 '13 at 11:54

3 Answers3

1

You can do some think like

<% @meets.each do |meet| %>
  <% meet_tags = meet.tags.split(' ') %>
  <% meet_tags.each do |meet_tag|%>
   <p class="MeetTags"><%= meet_tag %></p>
  <%end%>
<% end %>
Sabyasachi Ghosh
  • 2,765
  • 22
  • 33
0

Assuming that your tags are joined with space:

<% @meets.each do |meet| %>
  <% meet.tags.split(' ').each do |word| %>
    <p class="MeetTags"><%= word %></p>
  <% end %>
<% end %>
Marek Lipka
  • 50,622
  • 7
  • 87
  • 91
0

your problem is that meet.tags is an array, so you're telling rails to display the array: that's why you're seeing the square braces.

If you want to split tags into groups of 3, and then show them separated by spaces, you could do this:

<% @meets.each do |meet| %>
  <% tag_arr = meet.tags.split(' ') %>
  <% tag_arr.in_groups_of_3.each do |tag_subarr|%>
   <p class="MeetTags"><%= tag_subarr.reject(&:blank?).join(" ") %></p>
  <%end%>
<% end %>

I'm using .reject(&:blank?) because the in_groups_of method will pad the last subarray out with nil, which will mess up your formatting, and maybe cause other problems if you try and do anything with the member elements of tag_subarr.

Max Williams
  • 32,435
  • 31
  • 130
  • 197