· 1 min read
Screencast: Datenexport nach Excel oder CSV
Downloads in verschiedenen Formaten:
Resourcen:
config/application.rb
[ruby] require ‘csv’ [/ruby]
products_controller.rb
[ruby] def index @products = Product.order(:name) respond_to do |format| format.html format.csv { send_data @products.to_csv } format.xls # { send_data @products.to_csv(col_sep: “t”) } end end [/ruby]
models/product.rb
[ruby] def self.to_csv(options = {}) CSV.generate(options) do |csv| csv << column_names all.each do |product| csv << product.attributes.values_at(*column_names) end end end [/ruby]
config/initializers/mime_types.rb
[ruby] Mime::Type.register “application/xls”, :xls [/ruby]
views/products/index.xls.erb
[html]