Rails 3: How to display properly text from “textarea”?

前端 未结 9 1154
予麋鹿
予麋鹿 2021-01-31 15:31

In my Rails 3 application I use textarea to let users to write a new message in a forum.

However, when the message is displayed, all newlines look like spac

9条回答
  •  醉梦人生
    2021-01-31 16:10

    The following helper preserves new lines as line breaks, and renders any HTML or Script (e.g Javscript) as plain text.

    def with_new_lines(string)
       (h(string).gsub(/\n/, '
    ')).html_safe end

    Use as so in views

    <%= with_new_lines @object.some_text %>
    

提交回复
热议问题