Similar to this question except I don't use html_safe
anywhere in the whole project.
I generate a CSV file in index.csv.erb
like this:
<%=
response.content_type = 'application/octet-stream'
CSV.generate do |csv|
@persons.each do |person|
csv << [ person[:name], person[:nickname] ]
end
end
%>
PROBLEM: If nickname is NULL in the database (ActiveRecord/MySQL) then the CSV file associated element becomes ""
. I would expect ""
, or even nothing at all.
Result file sample:
Nicolas, Nico
Joe, ""
How can I prevent this from happening?
The problem here is that you're not using
html_safe
. Your nickname field is blank and converted to""
in the csv file, but it is deemed unsafe by Rails and html escaped.Just call
html_safe
on the result:The solution you linked to does not work anymore with Rails 3 because all strings are considered unsafe by default, which was not the case in Rails 2.
Refactored
Benoit is absolutely correct and thanks for that tip. After looking at your code I see a much cleaner approach to generating your CSV as well, which I thought I would share for those landing here (like me!):
Essentially, you don't need all that CSV generate stuff. Ruby can take an
Array
and turn it into a CSV string, then just use acollect
andjoin
to put it all together nicely.You can also do the following if you prefer having it on separate lines, which I do:
Here you'll need to use the
-%>
to ensure you don't get extra blank lines and you'll need to use therow_sep: nil
option so thatto_csv
doesn't add a\n
at the end of each line.Anyway, hope that helps clean some people's code up.