Hello, today I end up with strange issue in Rails 5:
Here is how a simple _text.html.erb_ file looks like:
<textarea><%= "a\nb\nc" %></textarea>
And here is what it produce:
I'm talking about these leading spaces before second and third strings.
In my code I have a form with textarea:
.form
=form_for article do |f|
.form-group
=field_label f, :text, true
=f.text_area :text, class: %w(form-control), rows: 20, placeholder: t('placeholder.article_text')
And the same issue happens there on Edit action
I found out that the reason is haml-rails gem in Gemfile. Without it <textarea><%= "a\nb\nc" %></textarea>
forks fine, but with it, it adds extra spaces.
I've run into the same issue when trying out the Rails 5 beta3. I've made a project that is a simple Rails 5 app that demonstrates the issue with haml 4.0.7 here:
https://github.com/remomueller/TextAreaSpacing
Note that the project does not have haml-rails in it. Hope that helps debugging, as I'd love to keep using Haml in Rails 5!
Here are some notes to launch the project:
To Recreate
Clone code:
git clone https://github.com/remomueller/TextAreaSpacing.git
cd TextAreaSpacing
bundle install
Launch Server
rails s
Open http://localhost:3000/items/new
Create item with
Name:
Name
Description:
My Description
Will have spaces in front of this line
Click "Create item"
Click 'Edit'
Extra spacing will be in front of 'Will have spacecs'
Just an FYI, this extra spacing can be fixed by doing the following on the form:
= f.text_area :description, value: item.description.to_s.gsub("\r\n", "\r")
Edit
Thanks to @droptheplot, looks like this can be fixed by doing the following as well in config/initializers/haml.rb
# config/initializers/haml.rb
require 'haml/template'
Haml::Template.options[:ugly] = true
@norman @amatsuda Any idea why this regression is occurring with Rails 5? Could be a coincidence with Ruby 2.3 maybe?
Just to isolate the issue I tried to run Rails 5 with Ruby 2.2 instead of 2.3 and the issue still occurs so something must have changed in Rails 5 to cause this.
This may be resolved by Haml 5 because it doesn't have ugly: false
mode. Please try Haml 5.0.0.beta.2 and reopen this if the problem still exists on Haml 5.
@k0kubun I've tested 5.0.0.beta.2 and as far as I can tell it fixes the issue.
I've submitted two pull request for haml-rails and html2haml to make them allow 5.0.0:
Ah, good catch. Thanks!
Most helpful comment
This may be resolved by Haml 5 because it doesn't have
ugly: false
mode. Please try Haml 5.0.0.beta.2 and reopen this if the problem still exists on Haml 5.