HAML: remove white space after "link_to"

krn picture krn · Apr 15, 2011 · Viewed 9.2k times · Source

The following code leaves a white space in HTML:

= link_to "Login", "#"

Normally, HAML allows to remove it by putting ">" at the end of the line, for example:

%input#query{:type => "text", :value => "Search"}>

However, that seems to be impossible, when Rails code is inserted.

How do I fix this?

Answer

Georgi picture Georgi · Feb 6, 2014

The solution with span is not ideal as it adds an unnecessary html tag that will require processing, if you want to avoid the <span> you should use HAML's succeed:

= succeed "," do
  = link_to "Login", "#"

which will result in the following HTML being rendered:

Login,

rather than

Login ,

Note that if you want to achieve the following result:

Login,Profile

i.e. no whitespace whatsoever between the comma and two links you should do the following:

= succeed link_to "Profile", '#' do
  = succeed "," do
    = link_to "Login", '#'

which gets pretty tedious