CSV.generate_line adds a new line character to the end of the line it creates, but also has the value <%= %> , so you get two new lines.
To suppress a new line character from the output of an erb expression, use this syntax: <%= -%>
So:
<%- headers = ["Id", "Name"] -%> <%= CSV.generate_line headers -%> <%- @customers.each do |n| -%> <%- row = [ n.id, n.fname ] -%> <%= CSV.generate_line row -%> <%- end -%>
The accepted answer goes to a new line generated from erb , but suppresses a new line from CSV.generate_line , which, in my opinion, is not the best way to do this.
source share