0

I have some HAML views in my Rails project that are used to send files to the user. They're not rendered as HTML, just plain text files that get downloaded. These files have to match a very specific format, and the format has an idiosyncrasy that ends up requiring every second line to end with a tab character.

Line 0a\t01234
Line 0b\t
Line 1a\t12345
Line 1b\t
Line 2a\t23456
Line 2b\t

The a lines have have their tab characters printed fine, but the b lines do not. If I add any non-whitespace characters after the tab character, the tab gets printed. But when it's the last character on the line, it does not.

My view looks like

- @line_pairs.each do |line_pair|
  = line_pair.a.words + "\t" + line_pair.a.numbers
  = line_pair.b.words + "\t"

I'm sure that the tab character is not there (my editor shows them visually). There also is no space or anything of the like. I just get

Line 0a\t01234
Line 0b
Line 1a\t12345
Line 1b
Line 2a\t23456
Line 2b

Is there any way to fix this? Thanks for any help.

  • http://stackoverflow.com/questions/9660987/how-to-get-a-tab-character – apneadiving Nov 24 '13 at 20:09
  • Thanks for the suggestion, but that doesn't work. The resulting file just has ` ` where the tab should be. – Benjamin Carlsson Nov 24 '13 at 20:39
  • Does it work if you set [the `ugly` option](http://haml.info/docs/yardoc/Haml/Options.html#ugly-instance_method) to true? (Rails should default to ugly in the production env.) – matt Nov 24 '13 at 20:56
  • 3
    (Having said that, even if using `ugly` does work I’d recommend just using ERB for this. Haml assumes you’re creating HTML, it isn’t really suited for something like this when you need fine control of whitespace.) – matt Nov 24 '13 at 20:59
  • 2
    Why are you using Haml for text files? It is not created for that. Just use ERb it would be much easier. – Hauleth Nov 24 '13 at 23:18

3 Answers3

3

The haml documentation says that tilde (~) acts just like = but preserves whitespace.

Does this work?

- @line_pairs.each do |line_pair|
  ~ line_pair.a.words + "\t" + line_pair.a.numbers
  ~ line_pair.b.words + "\t"
carols10cents
  • 6,943
  • 7
  • 39
  • 56
0

Try :preserve

:preserve
    - @line_pairs.each do |line_pair|
sam
  • 40,318
  • 2
  • 41
  • 37
0

The proper solution as pointed out by matt is actually to just use ERB, as HAML is not meant to fill needs around controlling whitespace at this level of detail.