0

I have a model Company, this model has a field Address. Company.first.address returns the following in the console:

"11 Local Street,\r\n" + "City,\r\n" + "POSTCODE"

This throws a spanner in the works when I try to export using

CSV.generate_line([company.name, company.address])

The address field spans more than one column and therefore makes the CSV not useful.

Can't seem to find a way to turn it into a single quoted string.

This is the full create.csv.erb:

<%- headers = [
              'name',
              'email',
              'address',
              'phone',
              'contract_date',
              'deal_percentage',
              'active',
              'editable'
              ] -%>
<%= (CSV.generate_line(headers)).strip %>
<% @companies.each do |company| %>
  <%= CSV.generate_line([
                        company.name,
                        company.email,
                        company.address,
                        company.phone,
                        company.contract_date,
                        company.deal_percentage,
                        company.active,
                        company.editable
    ], { quote_char: "\0", encoding: "UTF-8" }).strip %>
<% end %>

I'm using Ruby 2.4.0 and Rails 5.0.1.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Cromon MS
  • 11
  • 2
  • Can you post the exact code you're using? – whodini9 Mar 23 '17 at 18:18
  • 1
    This has nothing to do with commas. CSV values can contain commas and newlines, and `CSV` has no trouble automatically escaping them: `CSV.generate_line(['foo', 'foo,bar', "foo,\nbar"]) # => "foo,\"foo,bar\",\"foo,\nbar\"\n"` – user229044 Mar 23 '17 at 18:40
  • 2
    Why are you using `"\0"` as your quote character? You're explicitly telling `CSV` to use `"\0"` to quote cells that contain commas. – user229044 Mar 23 '17 at 18:43
  • When I remove the quote_char I get the same problem. Also this is giving me the + too 2.4.0 (main):0 > CSV.generate_line(['foo', 'foo,bar', "foo,\nbar"]) => "foo,\"foo,bar\",\"foo,\n" + "bar\"\n" Is there a way to format the address on the model level, I am fine with over-riding it. – Cromon MS Mar 23 '17 at 19:16
  • 3
    You shouldn't be generating the CSV in your view. That logic should be in your controller, where you assign the output to a variable, then access the variable in your view. – the Tin Man Mar 23 '17 at 19:53
  • @theTinMan could you point me in the direction of some more info about this please, can't seem to find any references where the CSV rows are actually generated in the controller. I see some where the model is used with a custom `to_csv` method, is this expected to deal with the address field differently than in the view? – Cromon MS Mar 24 '17 at 08:51
  • Rails is an [MCV](http://stackoverflow.com/a/101561/128421) system. The logic in a view should be simple conditional tests to determine what variables to display and to loop over arrays or hashes. The creation of those variables is done in the controller to keep the view clean. The [CSV documentation](https://ruby-doc.org/stdlib-2.4.1/libdoc/csv/rdoc/CSV.html) shows how to output to a string among other methods of generating output. You should be able to figure it out from that. The Rails tutorials go into this too. – the Tin Man Mar 24 '17 at 18:40

0 Answers0