문제

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