1

I am bumping into an issue in my Rails app where a .csv export of data from a table is not formatting correctly.

First of all, here is the code that generates the .csv export...

report_controller.erb

class Admin::ReportController < ApplicationController

    def index
        @report = Report.all

        respond_to do |format|
            format.html
            format.csv do
                headers['Content-Disposition'] = "attachment;
                filename=\"report-summary.csv\""
                headers['Content-Type'] ||= 'text/csv'
            end
        end
    end

end

index.csv.erb

<%- headers = ["Report Id", "Datetime", "Latitude", "Longitude", "Report Type"] -%>
<%= CSV.generate_line headers -%>
<%- @report.each do |report| -%>
    <%- row = [report.id, report.datetime, report.latitude, report.longitude, report.report_type.report_type] -%>
    <%= CSV.generate_line row -%>
<%- end -%>

As I was building this out, everything was working fine, until I got to the last column in the table, 'Report Type'. Here's what happens when that gets added:

Problem table

Notice how that first entry has the HTML &quot; leading and trailing? But the next one doesn't? And how the first one is distributed across additional headerless columns? And how the last one doesn't have the &quot; but does have &amp in the middle (for an ampersand, of course)?

All of these values are stored as text in a ReportType table and they come through as strings in Rails and, as far as I can tell, there is absolutely nothing different about their data types anywhere, yet they are behaving differently when pulled out of the table.

If I take 'Report Type' out of the export, see how lovely my table is?

Good table

No skipped lines or anything!

It seems to me that these HTML characters - especially the leading and trailing double quotes - are causing at least some of the trouble here. I tried using .gsub('"', '') to remove them, but this has no effect.

Has anyone encountered this before or have any insight here?

3
  • I think your CSV file is coming out of ERB with HTML escaping turned on. Are you sure there's nothing in the source data like that? Commented Jul 28, 2016 at 0:22
  • @tadman Hmm... not that I know of - I'm a bit new to Rails - where would I look to check? Commented Jul 28, 2016 at 0:53
  • @tadman - thanks man, you helped me get it - see below. Commented Jul 28, 2016 at 1:05

1 Answer 1

1

Thanks to @tadman for pointing me in the right direction on this - I changed this line:

<%= CSV.generate_line row -%>

by adding raw like so:

<%=raw CSV.generate_line row -%>

And that did the trick. Whew!

Still, if anyone has other solutions for this, I would like to hear them.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.