1

I used html_safe, raw and also sanitize. But I still characters like \r \n — this is because i use "return/enter" key in Mac to go to next line when i enter data to my description field - which is textarea. Is there anyway to avoid these in my view page.

actual result -

\r\ntext text text text text text

\r\n\r\n

this place is famous for blah blah blah

expected result-

text text text text text text

this place is famous for blah blah blah
Aleksei Matiushkin
  • 119,336
  • 10
  • 100
  • 160
Hayz
  • 53
  • 12

3 Answers3

0

Your options are to either strip the characters before you insert them into the DB, or else strip them in the view.

Either way, you can do this with gsub - your comment above shows that you were using gsub improperly (remember to post what you have tried in your question as well as the results!)

Example:

@attraction.description.gsub(/\r\n/,'')

This will only replace a carriage return followed by a newline, I suggest instead replacing any newline/carriage returns:

@attraction.description.gsub(/[\r\n]/,'')

If you want to replace with:

@attraction.description.gsub(/[\r\n]+/,'<br />').html_safe

And so forth..

On the other hand, if you are actually seeing the text "\r\n" in your output, then you have somehow converted the carriage returns and newlines into their "typable" counterparts, possibly with something like an str.inspect call before storing them?

If that's the case, then you want to replace not carriage returns and newlines, but the strings "\r" and "\n", for example:

@attraction.description.gsub(/\\[rn]/,'').html_safe

(You were putting your regexp in quotes for gsub, making it a string comparison as opposed to a regexp)

  • The description is a field of multiple lines and that why it isnt working. The way you said, works well in much smaller context- when i had only 2 strings separated by a newline. – Hayz Jul 11 '16 at 07:54
  • It should work with a string that has multiple newlines in it. If it does, please mark the answer as accepted, if it doesn't, please explain how it fails. – David Ljung Madison Stellar Jul 11 '16 at 08:08
  • I have data in my text area i.e @attraction.description = "

    There's no better way to complete a fabulous day at Park

    \r\n\r\n

    Weaved together by a tale of friendship and courage that will linger with you long after you leave our magical isle.

    \r\n\r\n

    The Storyline:


    \r\nExplore new dimensions Will a boy and girl find true friendship?\r\n\r\n

    The story continues

    " Then i do @attraction.description.gsub(/[\r\n]+/,'
    ').html_safe but it fails to show the exact result -
    – Hayz Jul 11 '16 at 08:32
  • And what result does it show? You really need to learn that to properly get help and/or write a bug report you need to exactly specify what you tried as well as what the results were, not just "it failed" or "it didn't work" – David Ljung Madison Stellar Jul 11 '16 at 08:43
  • If you already have the

    in the text, then just remove the carriage returns and newlines with the second gsub. And again, you said in one of your comments above that you were seeing *newlines* in your HTML, is that true? Or are you seeing the actual string "\n" and "\r"?

    – David Ljung Madison Stellar Jul 11 '16 at 08:44
  • the result is \r\n\r\n Weaved together by a tale of friendship and courage that will linger with you long after you leave our magical isle. \r\n\r\n The Storyline: \r\nExplore new dimensions Will a boy and girl find true friendship? \r\n\r\n The story continues i.e i can actually see the "\n" and "\r". – Hayz Jul 11 '16 at 08:47
  • Ah, then that's a different problem. I'll update my answer. – David Ljung Madison Stellar Jul 11 '16 at 08:53
  • 1
    thanks a lot David, it worked well :) Thank you sooo much for your time and help. I will accept your answer. – Hayz Jul 11 '16 at 09:01
0

Hope this may help you:

    %{ Multi-line
   string }.squish                   # => "Multi-line string"

" foo   bar    \n   \t   boo".squish # => "foo bar boo"

http://apidock.com/rails/String/squish

Atchyut Nagabhairava
  • 1,295
  • 3
  • 16
  • 23
  • Sorry, this didnt work. Please refer the accepted answer. But thanks for your time :) – Hayz Jul 11 '16 at 09:02
0

Well I've tested this online, not in my ruby so don't know if it works %100. But from what I've read. Your code saved \r\n\ as a text, and your expected answer shouldn't show them so you have to run the following command:

x.gsub(/[\\r\\n]/,'')

cause \ is used with r & n to define other characters so you first need to discard it first \\ shows the \ character.

  • \\n means "\n" string
  • \n means a new line
OMY
  • 402
  • 1
  • 8
  • 18