Rails csv.erb view includes spaces

I have an index.csv.erb file with the following code:

<% headers = ['PrL', 'PrG', 'UOM', 'Quantity', 'Eff.Date', 'End Date','Price','Disc', 'P', 'Gr/Net'] %>
<%= CSV.generate_line(headers).strip.html_safe %>
<% @pricelist_price_groups.each do |pricelist_price_group| %>
  <% start_at = pricelist_price_group.start_at.strftime("%d/%m/%y") unless pricelist_price_group.start_at.blank? %>
  <% end_at = pricelist_price_group.end_at.strftime("%d/%m/%y") unless pricelist_price_group.end_at.blank?  %>
<%= CSV.generate_line([pricelist_price_group.pricelist_id,
                         pricelist_price_group.price_group_id,
                         pricelist_price_group.sale_uom_id,
                         start_at,
                         end_at,
                         pricelist_price_group.price,
                         pricelist_price_group.disc_dollar,
                         pricelist_price_group.disc_percent,
                         pricelist_price_group.price_flag,
                         pricelist_price_group.gross_or_net]).strip.html_safe %>
<% end %>

I add any spaces like

<% headers = ['PrL', 'PrG', 'UOM', 'Quantity', 'Eff.Date', 'End Date','Price','Disc', 'P', 'Gr/Net'] %>

<%= CSV.generate_line(headers).strip.html_safe %>

This space is then written to the CSV file. Other cases include having two spaces as a tab before<%= CSV.generate_line

This should be because it is the idea that rails are rendering. If there is a way, can I still keep spaces to keep the code clean?

+4
source share
2 answers

I had the same problem and fixed it with this answer in another stack overflow question.

i.e. use stripfor title bar:

<%= CSV.generate_line(headers).strip%>

And .html_safe.stripfor the content:

<%= CSV.generate_line(row).html_safe.strip%>
+1

, .

<%- export = CSV.generate(" ", { headers: ["Array", "Of", "Headers"], write_headers: true, encoding: "UTF-8"}) do |csv| -%> 
<%- csv.add_row data -%> 
<%- end -%>
<%= export.lstrip.html_safe -%> 
+1

All Articles