0

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!

AnthonyGalli.com
  • 2,796
  • 5
  • 31
  • 80
  • Don't ever trust user input. Do not give them the ability to write html in your application. What kind of name uses four lines of input? Can you show me a sample of input you are expecting. – Tucker Apr 11 '15 at 23:04
  • @MenelikTucker `:name` isn't the best description. It's so users can basically write any content like a quote, fact, story that reflects their values. I want them though to be able to choose how to format their value. – AnthonyGalli.com Apr 11 '15 at 23:08

1 Answers1

1

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)

spickermann
  • 100,941
  • 9
  • 101
  • 131
  • I'm confused by your example. How does it relate to my example? Thanks Spickerman! – AnthonyGalli.com Apr 11 '15 at 23:31
  • @AnthonyGalli.com The example came directly from the documentation. I hope now it is more clear. – spickermann Apr 12 '15 at 00:02
  • I tried this: `<%= f.simple_format :name, rows: 4, class: 'form-control', id: "gold-standard", placeholder: 'Enter Value' %>`, f being @valuation, but I get an error. – AnthonyGalli.com Apr 12 '15 at 00:21
  • I read the documentation before I posted this question, but it didn't make sense to me and I was getting errors so I thought it wouldn't work. – AnthonyGalli.com Apr 12 '15 at 00:24
  • You do not have to change the `text_area` for the input. The user can already just add line breaks into that `text_area. You have to change the output, so that the line breaks get replaced by `
    `. Use the code above instead of `<%= @valuation .name %>` on the show page.
    – spickermann Apr 12 '15 at 00:57
  • Thank you for your patience! That's why i was so confused lol – AnthonyGalli.com Apr 12 '15 at 01:04