Rails: array.join with html in delimiter escaped

I have an array of strings containing unsaved content (user input).

I want to join these lines in my template, separated by <br /> .

I tried:

 somearray.join("<br />") 

But it will also save the spararator.

Is there a workaround, bearing in mind that the contents of the array must absolutely be escaped?

+4
source share
3 answers

Is there a reason why it should be a <br /> tag? Could you use a list?

 <ul> <% somearray.each do |item| %> <%= content_tag :li, item %> <% end %> </ul> 
+3
source

Have you tried this?

 raw somearray.join("<br />") 
+2
source

raw and h provide ways to selectively apply this default behavior.

 <%= raw user_values_array.map {|user_value| h user_value }.join('<br />') %> 

Even better, Rails 3.1 introduced safe_join(array, sep) for this purpose. Used with html_safe , it can do what you need.

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

Documentation

+1
source

Source: https://habr.com/ru/post/1400007/


All Articles