5
votes

I am building a ruby on rails application using simple form and haml for the views. However I've noticed an issue with regards to forms that have textareas. I didn't notice it at first however when I save some text in a textarea and dump it in a pre tag I've noticed that every line break is followed by a number of ugly white spaces. I'm not sure whats going on here. So if I enter something like the following in a textarea:

THIS IS ONE LINE
THIS IS ANOTHER LINE
THIS SHOULD NOT BE INDENTED
  THIS SHOULD BE INDENTED BY TWO SPACES

Outputting it into a pre tag gives:

  THIS IS ONE LINE
  THIS IS ANOTHER LINE
  THIS SHOULD NOT BE INDENTED
    THIS SHOULD BE INDENTED BY TWO SPACES

I'm pretty lost here - this is my basic textarea code - nothing much(added the tilde after hearing that it would help in removing unwanted spaces - didn't work at all):

~f.input :details, :label=>false, :input_html=>{:class=>'span12 input-code', :rows=>40}

Another observation - the spaces do not show up in the textarea if I render the text area using simple form - however if I manually code in a textarea to output the contents it shows the spaces. Could this have anything to do with the simpleForm gem?

1
Would you care to prepare a minimal rails app that does just this and put it on github (or whereever)?Sergio Tulentsev
When you say, "outputting in a pre tag" does that mean its doing what Stackoverflow does by outputting the text while you're writing it? Does your developer tools explain the margin? Is it because of your span12 or input-code rules?Trip
I've checked and its all spaces - the span12 and input-code those are just css classes - how can they have an influence?Ali
Did you find a solution for this issue? I have the same problem....Michael Koper

1 Answers

6
votes

I know this is a very old post but I came here looking for a solution to the exact same issue so I thought on sharing the solution I found on another link in case someone comes here too...

For me this happened on my Rails 5 app, that uses simple_form and HAML.

The reason of the additional spaces is HAML, and in order to avoid them, you need to add to the config/initializers/haml.rb the following line:

Haml::Template.options[:remove_whitespace] = true

Here's the source of my answer (check the very last comment by adavia): https://github.com/haml/haml/issues/643

And just in case, here's the list of options HAML allows: http://haml.info/docs/yardoc/Haml/Options.html

Edit

This option also works:

Haml::Template.options[:ugly] = true