This is a huge and long-standing problem in HAML. I personally solved this in two different ways.
1) I put the desired text in the elements of the array and use
join(', ')
But it's a pretty crappy way of doing things.
2) The only "solution" to HAML is a special> character. The> character removes spaces in the output html so that there are no spaces BEFORE or AFTER this tag is displayed.
This is the best I could think of, and I lose sleep at night because of this terrible disgrace.
%a{:href => 'here'} Github %span> , %a{:href => 'there'} Twitter
Notes on this latest technique.
a. If you are not using & nbsp; you will not get a space, even if it is included in the source code. Using quotes (% span> # {","}) and a space do not work, as it is obvious that HAML truncates the output.
b. You should use the tag because using> only works after the tag, as far as I can tell. Using =>, unfortunately, will not work. This would be my recommended way to do this, but it would not solve the last lost area anyway.
3) In my rails projects, I use the following combination of partials and helpers:
partial: _haml_comma.html.haml
%span> ,
helper.rb
def comma render :partial => "shared/haml_comma" end
.haml file:
%a{:href => 'here'} Github = comma %a{:href => 'there'} Twitter
4) If you have ENUMERABLE and LOOPING, the following auxiliary and partial
application_helper.rb
views / shared / _comma_separate.html.haml:
= succeed i + 1 < total ? ', ' : nil do %span.some_class< = yield
in your opinion:
- enumerable.each_with_index do |w, i| = comma_separate i: i, total: enumerable.size do %b = w
(This change was made in 2019, 5 years after the first comment. Why is this not resolved by the accompanying HAML?)