开发者

Showing linebreaks in Rails3

开发者 https://www.devze.com 2023-01-31 21:15 出处:网络
I 开发者_JAVA技巧store the linebreaks as \"line\\n\\nline\" in the database. When i am displaying it, I convert it using this method:

I 开发者_JAVA技巧store the linebreaks as "line\n\nline" in the database.

When i am displaying it, I convert it using this method:

  def showLineBreaks(from_textarea) 
   from_textarea.gsub(/\n/,"<br/>")
  end

But these renders the text as

line<br><br>line 

instead of showing the linebreaks.

What is the right way to do this?


You probably need to flag your content as html_safe for it to display properly, otherwise the view will render it as the string should be displayed.

<%= showLineBreaks.html_safe %>


If you're trying to display newlines saved from text areas, you could do the following in your view:

<%= simple_format from_textarea %>

No need to do manual substitution in this case.

0

精彩评论

暂无评论...
验证码 换一张
取 消