Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input from textarea has weird spaces prepended to each line in simple form and haml

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?

like image 557
Ali Avatar asked May 22 '13 12:05

Ali


1 Answers

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
like image 65
Marco Avatar answered Sep 24 '22 20:09

Marco