Denis Kreshikhin
Denis Kreshikhin

Reputation: 9410

How to properly concatenate string and tag result?

I am making a rails helper for creating of a login button with a text and a logo image. If I put as content only a text or image_tag result it works very well.

def test_helper
  anchor = content_tag :a, "enter by", :href => '#'
  concat content_tag :div, anchor, :class => 'login'
end

# result:
# <div class="login"><a href="#">enter by</a></div>

def test_helper
  anchor = content_tag :a, image_tag("logo.png"), :href => '#'
  concat content_tag :div, anchor, :class => 'login'
end

# result:
# <div class="login"><a href="#"><img src="assets/logo.png" /></a></div>

But when I try to pass result of concatenation it is returned an img tag with escape symbols in html source:

def test_helper
  anchor = content_tag :a, "enter by" + image_tag("logo.png"), :href => '#'
  concat content_tag :div, anchor, :class => 'login'
end

<div class="login"><a href="#">enter by&lt;img src=&quot;/assets/logo.png&quot; /&gt;</a></div>

How to properly concatenate a string and a result of content_tag?

Upvotes: 0

Views: 2371

Answers (1)

PinnyM
PinnyM

Reputation: 35533

The problem is because of the concatenation being done when building the anchor_tag. You need to call html_safe on the string literal to avoid the escaping:

anchor = content_tag :a, "enter by".html_safe + image_tag("logo.png"), :href => '#'

Upvotes: 4

Related Questions