Damir Nurgaliev
Damir Nurgaliev

Reputation: 351

How to wrap the returns of method to one div block

I'm creating like model in rails application. To use images in tooltip I need to wrap them all to:

<div class="tooltip">

The main method is:

  def like_user_avatars
    likes.map(&:user_avatar).map { |avatar| image_tag(avatar,
    class: "tooltip_avatars") }.join
  end

It generate something like this: <img src='...'><img src='...>. I need to wrap them all to one div block named tooltip, it should looks like <div class="tooltip"><img src='...'><img src='...></div>. I tried to add content tag :div, class: "tooltip", but it doesn't wrap everything. How can I solve my problem?

Upvotes: 2

Views: 83

Answers (1)

Alex Kojin
Alex Kojin

Reputation: 5204

Use concat to combine all images and wrap them into parent div

def like_user_avatars
  content_tag :div, class: 'tooltip' do
    likes.each do |like| 
      concat image_tag(like.user_avatar.avatar, class: "tooltip_avatars")
    end
  end
end

More about contact and his usage

Upvotes: 2

Related Questions