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

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

    Rails got a helper method out of the box, so you dont have to write your own method.

    From the documentation:

    simple_format(text, html_options={}, options={})

    my_text = "Here is some basic text...\n...with a line break."
    
    simple_format(my_text)
    # => "

    Here is some basic text...\n
    ...with a line break.

    " more_text = "We want to put a paragraph...\n\n...right there." simple_format(more_text) # => "

    We want to put a paragraph...

    \n\n

    ...right there.

    " simple_format("Look ma! A class!", :class => 'description') # => "

    Look ma! A class!

    "

提交回复
热议问题