refactoring
Benoit is absolutely right and thanks for this tip. Having looked at your code, I see a much cleaner approach to generating your CSV, which, as I thought, I would share for those who have landed here (for example, me!):
<%= response.content_type = 'application/octet-stream' @persons.collect{ |person| [ person[:name], person[:nickname] ].to_csv }.join.html_safe %>
In fact, you do not need everything that CSV generates. Ruby can take an Array and turn it into a CSV string, and then just use collect and join to make things good.
You can also do the following if you prefer it on separate lines, which I do:
<% response.content_type = 'application/octet-stream' -%> <% @persons.each do |person| -%> <%= [ person[:name], person[:nickname] ].to_csv( row_sep: nil ).html_safe %> <% end -%>
Here you need to use -%> to make sure you are not getting extra empty lines, and you need to use the row_sep: nil option so that to_csv does not add \n to the end of each line.
In any case, I hope this helps cleanse some people.
source share