Вопрос

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!

Это было полезно?

Решение 2

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.

Другие советы

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 %>

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 %>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top