Sam Kong
Sam Kong

Reputation: 5810

rails 3 html_safe confusion

How do I achieve this?

In a controller

@arr = ["<one>", "<two>"]

In a view (haml)

= @arr.join("<br>")

As you guess, "<br>" shouldn't be escaped. So the result would be like the following.

&lt;one%gt;<br>&lt;two%gt;

How do I do that?

Thanks.

Sam

Upvotes: 0

Views: 1942

Answers (2)

Chap
Chap

Reputation: 3553

Rails has this built in.

<%= safe_join(@arr, "<br />".html_safe) %>

Upvotes: 5

Zabba
Zabba

Reputation: 65467

You could roll your own:

arr = ["<one>", "<two>"]
''.html_safe.tap {|x|
  arr.each_with_index { |el, ix|
    x << el
    x << raw("<br/>") if ix < arr.size-1
  }
}

Also look at the Array.join code in Rails

Upvotes: 1

Related Questions