每天一剂Rails良药之Rendering CSV From Your Actions

news/2024/7/21 7:12:00 标签: Rails, Excel, Windows
有时候我们需要输出Comma Separated Values(CSV)等各种形式的输出来满足用户的需要:
[code]
class ExportController < Application ontroller
def orders
content_type = if request.user_agent =~ /windows/i
'application/vnd.ms-excel'
else
'text/csv'
end

CSV::Writer.generate(output = "") do |csv|
Order.find(:all).each do |order|
csv << [order.id, order.price, order.purchaser, order.created_at]
end
end

send_data(output, :type=> content_type, :filename => "orders.csv")
end
end
[/code]

http://www.niftyadmin.cn/n/728925.html

相关文章

synchronized 关键字和 volatile 关键字的区别

synchronized 关键字和 volatile 关键字的区别synchronized关键字和volatile关键字是两个互补的存在&#xff0c;而不是对立的存在。 volatile关键字是线程同步的轻量级实现&#xff0c;而synchronized是线程同步的重量级实现&#xff0c;从性能上看&#xff0c;肯定是volatile…

群集服务验证失败_一文解码:如何解决SSL/ TLS证书服务的高可用性?

2017年1月份&#xff0c;Google将Chrome 56中所有的 http站点标记为不安全&#xff0c;并对所有使用http方式的登录交互页发出醒目的“不安全”提示。Apple自2015年WWDC大会开始&#xff0c;就在计划逐步推进实施ATS(App与后台服务器通讯强制使用https通讯)的实施&#xff0c;并…

每天一剂Rails良药之Make your URLs Meaningful(and pretty)

这篇内容没什么价值&#xff0c;《Agile Rails》里的Routing Request一节讲述的比较详细了 [code] ActionController::Routing::Routes.draw do |map|map.connect :controller/service.wsdl, :action > wsdmap.connect :controller/:action/:idmap.connect :user, :controll…

python保存模型与参数_基于pytorch的保存和加载模型参数的方法

当我们花费大量的精力训练完网络&#xff0c;下次预测数据时不想再(有时也不必再)训练一次时&#xff0c;这时候torch.save(),torch.load()就要登场了。保存和加载模型参数有两种方式&#xff1a;方式一&#xff1a;torch.save(net.state_dict(),path):功能&#xff1a;保存训练…

每天一剂Rails良药之Stud Out Authentication

登录和认证常常是我们Rails系统所必需的&#xff0c;但经常不是程序的核心功能 我们可以在ApplicationController里定义logged_in?方法: [code] def logged_in?local_request? end helper_method :logged_in? [/code] 这样我们就可以在我们的Rails系统中的任何地方使用logg…

每天一剂Rails良药之Convert Your Sessions To ActiveRecord

我们看看config/environment.rb文件&#xff0c;其中有以下一段: [code]# Use the database for sessions instead of the file system# (create the session table with rake db:sessions:create)# config.action_controller.session_store :active_record_store [/code] Rai…

运算符示例

运算符示例1 点运算符 .2 字符集3 否定字符集4 重复次数4.1 * 号4.2 号4.3 &#xff1f;号4.4 {} 号4.5 (...) 特征标群4.6 | 或运算符4.7 \ 转码特殊字符4.8 锚点4.8.1 ^号4.8.2 $号1 点运算符 . . 是元字符中最简单的例⼦。 . 匹配任意单个字符&#xff0c;但不匹配换⾏符。…