Ruby on Rails CSV 将“"”而不是实际的报价
我正在尝试生成 CSV 文件。除了空白字段之外,一切都很好,我不太确定是否有 ""
而不是实际的引号。我提供了用于生成文件和一些输出的代码。
<% 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 ,[email protected],""
Brian,Oliveri Design ,[email protected],727-537-9617
LocalShopperJenn,Jennifer M Gentile ,[email protected],""
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 %>
Output
Username,Name,E-mail,Phone Number
admin,LocalShopper ,[email protected],""
Brian,Oliveri Design ,[email protected],727-537-9617
LocalShopperJenn,Jennifer M Gentile ,[email protected],""
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要在数组的每个部分调用 html_safe,然后从中创建一个新的(非 html 安全)字符串,而是尝试在从
generate_line
返回字符串之后在末尾调用它: >更新:为了安全起见,您需要确保此模板不是作为 HTML 发送到浏览器,而是作为原始 text/csv 文件发送到浏览器。如果行内容包含任何实际的 HTML 标记,如
,这些标记不会被转义,因为您已将输出声明为“安全”。
如果这个内容需要在HTML页面中输出,那么你最好考虑正确的转义,而不是像这样绕过它。
考虑一下您是否确实需要
html.erb
模板来生成 CSV。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
:UPDATE: For security, you need to be sure that this template is not being sent to the browser as HTML, but a raw text/csv file. If the row content contains any actual HTML tags like
<script>
, these would not get escaped, because you've declared the output as "safe".If this content needs to be output within an HTML page, then you'd better consider correct escaping instead of bypassing it like this.
Consider if you really need a
html.erb
template for generating CSV.这是我使用过的一个效果很好的模板:
如果您愿意,您可以完全在控制器内执行此操作,并将其呈现为
:text
类型。如果您在控制器内将内容按顺序排列(在本例中是一个简单的
@report
哈希),也比在视图中完成所有繁重的工作更有帮助。Here's a template I've used that works well enough:
You can do this entirely within the controller if you like and render it as type
:text
instead.It also helps if you wrangle the content into order, in this case a simple
@report
hash, inside the controller than to do all the heavy lifting in the view.