更改 FastCSV 的输出
我目前有一个控制器,它将处理使用 FasterCSV gem 将表导出到 CSV 文件的调用。问题是数据库中存储的信息有时不清楚,所以我想更改特定列的输出。
例如,我的 project.status 列有数字而不是状态,即数据库中的 1 对应于“活动”,2 表示“非活动”,0 表示“尚未决定”。当我导出表时,它显示 0,1,2,而不是“活动”、“非活动”或“尚未决定”。知道如何实现吗?
我尝试了一个简单的循环,该循环将检查最终生成的 CSV 文件并将每个 0,1,2 更改为其相应的输出,但问题是具有 0,1,2 的其他所有列也会更改。我不知道如何隔离该列。 提前致谢
def csv
qt = params[:selection]
@lists = Project.find(:all, :order=> (params[:sort] + ' ' + params[:direction]), :conditions => ["name LIKE ? OR description LIKE ?", "%#{qt}%", "%#{qt}%"])
csv_string = FasterCSV.generate(:encoding => 'u') do |csv|
csv << ["Status","Name","Summary","Description","Creator","Comment","Contact Information","Created Date","Updated Date"]
@lists.each do |project|
csv << [project.status, project.name, project.summary, project.description, project.creator, project.statusreason, project.contactinfo, project.created_at, project.updated_at]
end
end
filename = Time.now.strftime("%Y%m%d") + ".csv"
send_data(csv_string,
:type => 'text/csv; charset=UTF-8; header=present',
:filename => filename)
end
I currently have a controller that will handle a call to export a table into a CSV file using the FasterCSV gem. The problem is the information stored in the database isn't clear sometimes and so I want to change the output for a particular column.
My project.status column for instance has numbers instead of statuses ie 1 in the database corresponds to Active, 2 for Inactive and 0 for Not Yet decided. When I export the table it shows 0,1,2 instead of Active, Inactive or Not Yet decided. Any idea how to implement this?
I tried a simple loop that would check the final generated CSV file and change each 0,1,2 to its corresponding output, but the problem is every other column that had a 0,1,2 would change as well. I'm not sure how to isolate the column.
Thanks in advance
def csv
qt = params[:selection]
@lists = Project.find(:all, :order=> (params[:sort] + ' ' + params[:direction]), :conditions => ["name LIKE ? OR description LIKE ?", "%#{qt}%", "%#{qt}%"])
csv_string = FasterCSV.generate(:encoding => 'u') do |csv|
csv << ["Status","Name","Summary","Description","Creator","Comment","Contact Information","Created Date","Updated Date"]
@lists.each do |project|
csv << [project.status, project.name, project.summary, project.description, project.creator, project.statusreason, project.contactinfo, project.created_at, project.updated_at]
end
end
filename = Time.now.strftime("%Y%m%d") + ".csv"
send_data(csv_string,
:type => 'text/csv; charset=UTF-8; header=present',
:filename => filename)
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这实际上相当容易。在您的控制器代码中:
然后在您的模型代码中。不过,您可能已经有了一种将数据库状态解码为更具描述性的方法:
您可能可以通过多种方法来重构它。至少在控制器中,最好使该查找器成为更具描述性的命名范围。模型中的常量可以引入 SettingsLogic 配置或其他类似的 gem。
This is actually fairly easy. In your controller code:
Then in your model code. You probably already have a method that decodes the DB status to a more descriptive one though:
There are probably a number of ways you can then refactor this. In the controller at least, it would probably be best to make that finder a more descriptive named scope. The constants in the model could be brought into SettingsLogic configuration or another similar gem.