ERB Template removing the trailing line ERB Template removing the trailing line ruby-on-rails ruby-on-rails

ERB Template removing the trailing line


To enable trim mode you have to instantiate the ERB object with '-' as the third parameter

ERB.new(template, nil, '-')


I had to combine the answers by willmcneilly, RobinBrouwer and fbo.

enable trim mode

ERB.new(File.read(filename), nil, '-')

Change to -%>

<% $things.each do |thing| -%>  <object name="<%= thing.name %>">    <type><%= thing.name %></type>  </object><% end -%>

And finally, convert from dos to unix. I used the following in Vim:

:set fileformat=unix:w


Try this:

Name: <%= @user.name %><% unless @user.phone.blank? -%>Phone: <%= @user.phone %><% end -%>Address: <%= @user.address %>

Also, don't know if this will work:

Name: <%= @user.name %><%= "Phone: #{@user.phone}" if @user.phone.present? -%>Address: <%= @user.address %>

If that doesn't work either, this should do the trick:

Name: <%= @user.name %><%= "\nPhone: #{@user.phone}" if @user.phone.present? %>Address: <%= @user.address %>