Array.join(“\n”) not the way to join with a newline?

后端 未结 7 1542
遥遥无期
遥遥无期 2020-12-29 01:44

I have an array..

[1,2,3,4]

and I want a string containing all the elements separated by a newline..

1

2

3

4
         


        
7条回答
  •  再見小時候
    2020-12-29 02:31

    As some of the other answers above imply, Rails may be escaping your code before rendering as html. Here's a sample that addresses this problem (first sanitizing the inputs, so that you can "safely" call html_safe on the result):

    my_array = [1, 2, 3, 4]
    my_array.map{ |i| i.to_s.sanitize }.join("\n").html_safe
    

    You only need sanitize if you don't trust the inputs.

提交回复
热议问题