7

I am able to export table data to a CSV file, however there is a blank row after every record. Why and how do i fix it?

in index.html.erb

<%= link_to "Export to csv", request.parameters.merge({:format => :csv})%>

in index.csv.erb

<%- headers = ["Id", "Name"] -%>
<%= CSV.generate_line headers %>
<%- @customers.each do |n| -%>
<%- row = [ n.id, n.fname ] -%>
<%= CSV.generate_line row %>
<%- end -%>
4
  • 1
    I tried your code and it seems to work. No blank row after every record. Maybe you need to inspect n.fname, you could have a new line char in the content of the field. Oct 18, 2011 at 19:23
  • @MatteoAlessani Doesn't seem to be in the database records. Anywhere else i can look? Can I just strip new lines out of the string before generating the row?
    – ctilley79
    Oct 18, 2011 at 19:46
  • yes, you can try to strip them, or the field. Oct 18, 2011 at 20:45
  • @MatteoAlessani I fixed it. This line did the trick. I think it has something to do with ruby's switch to fastercsv in ruby 1.9. this line forces tab delimited values. <%= CSV.generate_line row, :row_sep => ?\t, :quote_char => ?\ %>
    – ctilley79
    Oct 18, 2011 at 21:28

5 Answers 5

12

CSV.generate_line adds a new line character to the end of the line it generates, but so does <%= %> so you're getting two new lines.

To suppress the new line character from the erb expression output 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 leaves in the new line generated from the erb but suppresses the new line from CSV.generate_line which I think is not the best way to do it.

2
  • This is the perfect answer. I had initially resolved this issue with the use of .html_safe.strip. This worked until I had a double-quoted string with a comma in it. It messed up all my columns and was at a loss as to the solution until coming across this answer. My only suggestion is that you do not need the - on every line, it only has to be on the same line that has the =. Thank you so much!
    – MTarantini
    Sep 9, 2016 at 14:55
  • this really should be the accepted answer. much cleaner Jun 22, 2018 at 1:16
6

This is what fixed it.

<%= CSV.generate_line row, :row_sep => ?\t, :quote_char => ?\ %>
3
  • I'd like to know more about that encoding. Why the question mark? Sep 28, 2014 at 15:45
  • From tutorialspoint.com/ruby/ruby_variables.htm - You can get the integer value corresponding to an ASCII character or escape sequence by preceding it with a question mark. Sep 30, 2014 at 23:28
  • This method adds an extra '0' at the end of each line for some reason. Any idea why this might be happening?
    – mylescc
    Feb 27, 2015 at 9:47
2

for me following use of strip and html_safe worked, applied after the row was generated:

<%- headers = ["Id", "Tour No"] -%>
<%= CSV.generate_line(headers).strip%>
<%- @tours.each do |t| -%>
<%-   row = [ t.id,t.tour_no] -%>
<%= CSV.generate_line(row).html_safe.strip%>
<%- end -%>
0

Try this:

row = [ n.id, n.fname.strip ]

strip will remove \r and/or \n which might cause the blank lines. I dont have any other explaination your sourcecode is okay!

0
0

I thought I would chip in with my solution as just been struggling with this.

Basically on my local machine (a mac) everything was working absolutely fine, then when I deployed my app to heroku it would start adding in these mystery new rows.

For me the solution was to use:

CSV.generate_line(row, row_step: ?\r).html_safe

As a note, I'm using rails 4.1.6.

Hope that helps some other people struggling

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.