Search code examples
htmlrubyline-breakshtml-safe

How to permit <br> with text?


How can we give the user the ability to break-lines here?

<%= f.text_area :name, rows: 4, class: 'form-control', id: "gold-standard", placeholder: 'Enter Value' %>

Ideally only <br> would work out of the html elements, but worst case how can we permit all html elements, like <b>, <u>, etc?

For the latter case I tried using :name.html_safe or text_area.html_safe but those gave me errors. Thanks!


Solution

  • If it is just about <br> I would use the simple_format helper method.

    If you user enters "Here is some basic text...\n...with a line break." as a name, then you can output that line break as a <br> like this:

    <%= simple_format(@valuation.name) %>
    # => "<p>Here is some basic text...\n<br />...with a line break.</p>"
    

    If you need more complex things like <strong>, <ul>, <hx> I would consider allowing users to use a markup language like Markdown (e.g. with the Kramdown gem)