`
hideto
  • 浏览: 2650816 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

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

    博客分类:
  • Ruby
阅读更多
有时候我们需要输出Comma  Separated Values(CSV)等各种形式的输出来满足用户的需要:
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
分享到:
评论

相关推荐

    to_csv-rails:导出到 csv 文件

    导出为CSV on Rails的 这个简单的插件使您能够调用 to_csv 到一系列活动记录。 除了 :include 之外,构建器选项与 to_json / to_xml 相同。入门 在您的 Gemfile 中: gem 'to_csv-rails' # Last officially released...

    Rails之道,完整扫描版

    《Rails之道》按照Rails的各个子系统进行组织编排,分别介绍了Rails的环境、初始过程、配置和日志记录,Rails的分配器、控制器、页面生成和路由,REST、资源和Rails,ActiveRecord的基础、关联、验证和高级技巧,...

    Build your own ruby on rails web application

    Build your own ruby on rails web application

    Ruby-Rails实战之B2C商城开发

    Rails实战之B2C商城开发

    Rails.Recipes(Rails.3.Edition,2012) 英文版PDF

    In addition, you’ll find half the book is stocked with new eye-opening solutions to such common problems as how to extend Rails, test and deploy your sites, or add a web service to your actions....

    Learning Rails.pdf

    While most books written about Rails cater to programmers looking for information on data structures, Learning Rails targets web developers whose ...you'll be well on your way to becoming a Rails guru.

    [Rails] Crafting Rails Applications (英文版)

    This pioneering book is the first resource that deep dives into the new Rails 3 APIs and shows you how use them to write better web applications and make your day-to-day work with Rails more ...

    ParseCSV:Rails应用解析CSV文件

    在项目目录中运行: $rails s我的方法思考过程当我得到问题陈述时,这些是我最初的指导性问题: 从用户的角度来看,真正的问题是什么? 成功会是什么样? 我需要测试什么才能确保其正常工作? 我将如何对数据建模?...

    Rails之道.pdf(最新版)

    《Rails之道》详细讨论了Rails的程序代码并通过分析Rails中的代码片段来深入解释它的功能,同时,《Rails之道》部分章节也摘录了一些API文档中的内容,使读者能够快速地找到对应的API文档、相关的示例代码以及深入的...

    Rails Recipes英文版(清晰文字pdf+源码)

    Ruby三神书之一(其余的两本是Agile.Web.Development.with.Rails和Ruby For Rails,在我的资源列表也有) Rails is large, powerful, and new. How do you use it effectively? How do you harness the power? And, ...

    Rails之道.pdf 高清 带书签

    Rails之道.pdf 高清 带书签

    Rails for .NET Developers

    We have learned a lot of valuable lessons from Rails that we’ve applied to our .NET development too; if you take anything away from this book, it will be a new way of thinking about software ...

    Rails.Angular.Postgres.and.Bootstrap.2nd.Edition

    Also new is Webpack coverage, to develop the front-end code for your Rails application. Create a usable and attractive login form using Bootstrap's styles, while ensuring the database table backing ...

    rails 2.3.2离线安装rails 2.3.2离线安装

    rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails 2.3.2离线安装rails ...

    Learning Rails 5(高清文字pdf版)

    If you’re a web developer or designer ready to learn Ruby on Rails, this hands-on guide is the ideal way to get started. Rather than toss you into the middle of the framework’s Model-View-Controller...

    Ajax on Rails

    If you've already started working with Rails and seek to deepen your skill set, you'll find dozens of examples drawn from real-world projects, exhaustive reference for every relevant feature, and ...

    Bootstrap for Rails (2015)

    Fetching remote content from a Bootstrap modal 123 Using Bootstrap's modal in a Rails application 125 Summary 128 Chapter 9: Creating Image Slideshows with Bootstrap Carousel 129 Getting started with ...

    Rails项目源代码

    一个用Ruby on Rails搭建的图片分享的网站项目.完整源代码

Global site tag (gtag.js) - Google Analytics