How to get rid of blank spaces in templates rendered by Rails 3.2.13 ERB/Erubis?

Whatever I do I get a response page rendered like this:

Every page is rendered with blank spaces. Even if I put HTML and ERB tags together inline:

## Not together inline, blank spaces are like on the picture above:

<%= @foo %>

## Together inline, see the result lower:

<%= @foo %>

*## Result from t**ogether inline. *Blank spaces exists either but now look like this:

“” ## see the difference? Two double quotes together, not with a space in-between. But anyway it is rendered!



What I’ve learned so far:

The text from Rails guide:

config.action_view.erb_trim_mode gives the trim mode to be used by ERB. It defaults to ‘-’. See the ERB documentation for more information.

But at the same time they say that the Erubis is the default template engine in Rails, not ERB:

NOTICE: Rails 3 adopts Erubis as default default engine. You don’t need to do anything at all when using Rails 3. This section is for Rails 2.

And here is a code from gems\actionpack-3.2.13\lib\action_view\template\handlers\erb.rb:

    # Specify trim mode for the ERB compiler. Defaults to '-'.
# See ERB documentation for suitable values.
class_attribute :erb_trim_mode
self.erb_trim_mode = '-' ### yes, trim mode is set by dafault. But it is nothing for Erubis.
# Default implementation used.
class_attribute :erb_implementation
self.erb_implementation = Erubis ### yes, Erubis is instead of ERB
:escape => (self.class.escape_whitelist.include? template.mime_type),
:trim => (self.class.erb_trim_mode == "-") ### so :trim is true, just because self.erb_trim_mode = '-'. That's all.


From official Erubis docs:

Erubis deletes spaces around ‘<% %>’ automatically, while it leaves spaces around ‘<%= %>’. If you want leave spaces around ‘<% %>’, add command-line property ‘–trim=false’. Or add option :trim=>false to

So it is obvious that :trim => (self.class.erb_trim_mode == "-") will never remove spaces around <%= %>. Because there is no option in Erubis for it. And this is exactly seen on the picture above.

This line self.erb_implementation = Erubis informs that Rails implements Erubis instead of ERB.

But as written above the only trim mode Erubis supports is around <% %> , just pass true or false only. So any experiments with true ERB options (< % > -) yields nothing. Because Erubis just doesn’t support them.

My question: How to get rid of those blank spaces without switching to Slim, Haml, etc?

Why Rails say in their guide See the ERB documentation for more information if it means nothing for default Erubis?

I’m either reading this wrong, or something isn’t making sense. If I read this correctly, you have the following in a html.erb file:

<%= @foo %>

and it is rendering the following html:


If that’s correct, there are a couple of things. First, the rendering procedures would not have defined a class for the div tag, either that’s a misprint, or you must have had it in the template code.

Second, the <%= … %> does not insert any spaces or new line characters on its own, nor does it eliminate any. It evaluates the expression and returns it. For example, if @foo is a string with the value ‘aaa’, the template code above would return:


In the code:

<%= @foo %>

it would render:


If I’m reading your post correctly, the value passed by @foo must include newline characters and the quotation marks. You are correct that erubis strips spaces and newline characters from <% … %> statements and the config.action_view.erb_trim_mode no longer really does anything.