Ruby on Rails CSV put& quot;& quot;而不是实际的报价 [英] Ruby on Rails CSV putting "" instead of actual quotes

查看:145
本文介绍了Ruby on Rails CSV put& quot;& quot;而不是实际的报价的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试生成一个CSV文件。一切都很好,除了空白字段,我不太确定有& quot;& quot; 而不是实际的报价。我提供了我用来生成文件和一些输出的代码。

I am attempting to generate a CSV file. Everything is fine except for blank fields, I'm not quite sure have "" instead of actual quotes. I've provided the code I'm using to generate the file and some output.

<% headers = ["Username", "Name", "E-mail", "Phone Number"] %>
<%= CSV.generate_line headers %>

<% @users_before_paginate.each do |user| %>
  <% row = [ "#{user.username}".html_safe ] %>
  <% row << "#{user.profile.first_name} #{user.profile.last_name}".html_safe unless user.profile.blank? %>
  <% row << "#{user.email}".html_safe unless user.profile.nil? %>
  <% row << "#{user.profile.phone}".html_safe unless user.profile.nil? %>
  <%= CSV.generate_line row %>
<% end %>

输出

Username,Name,E-mail,Phone Number

  admin,LocalShopper ,shoplocally1@gmail.com,&quot;&quot;
  Brian,Oliveri Design ,brian@oliveridesign.com,727-537-9617
  LocalShopperJenn,Jennifer M Gentile ,localshopperjenn@hotmail.com,&quot;&quot;


推荐答案

而不是在数组的每个部分调用html_safe然后从中创建一个新的(非html安全的)字符串,在从 generate_line

Instead of calling html_safe on each part of the array and then making a new (non-html-safe) string from it, try calling it at the end, after the string is returned from generate_line:

<%= CSV.generate_line(row).html_safe %>

这篇关于Ruby on Rails CSV put&amp; quot;&amp; quot;而不是实际的报价的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆