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

前端 未结 9 1192
予麋鹿
予麋鹿 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:25

    Since simple_format does not do what you want, I'd make a simple helper method to convert newlines to
    s:

    def nl2br(s)
      s.gsub(/\n/, '
    ') end

    Then in your view you can use it like this:

    <%= nl2br(h(@forum_post.message)) %>
    

提交回复
热议问题