Search code examples
javascriptruby-on-railsformstextace-editor

Why is the Indentation Disappearing from User Submissions on My Site?


There are text areas on my site where users can submit (code) entries. When they visit the page later, it will display their submission. This has worked fine, but recently some of the entries fail to display the correct tabs and newlines when loaded. I looked at the data in Rails, and it looks like certain entries are missing the tab marks (\t) that should be there. It looks like the tabs have been replaced with whitespace, which doesn't show up when the text is displayed. What could be causing this error and how can I fix it?

Example Text that was Saved Correctly:

"lots of text;\r\n\r\some more text {\r\n\r\n\tsome code here(int a, int b{\r\n\t\t\tif(a%2==0)"

Example of Mysterious Problematic Text:

" some text\r\n    some more text\r\n    come code\r\n    bla bla\r\n        end end\r\n     "

Note: The text is entered in Ace editor, but transferred to a hidden text_area when the user clicks on Submit.

I have not been able to replicate this error when running locally in development. It has only happened to certain submissions in production (on Heroku).

Update: The hidden text_area sometimes displays the text with whitespace, but the ace editor doesn't display it correctly in its div.


Solution

  • How do you set value to ace? If you use editor.setvalue(str) str=editor.getvalue() string can't change.

    But if you set text into dom element and call ace.edit you have to be careful.