export_excel/app/controllers/products/products_controller.rb
class ProductsController < ApplicationController
def index
@products = Product.order(:name)
respond_to do |format|
format.html
format.csv { send_data @products.to_csv }
format.xls
end
end
end
export_excel/app/models/product.rb
require 'csv'
class Product < ActiveRecord::Base
def self.to_csv(options = {})
CSV.generate(options) do |csv|
csv << column_names
all.each do |product|
csv << product.attributes.values
end
end
end
end
索引文件位于:
export_excel/app/views/products/index.html.erb
部分在:
export_excel/app/views/products/_product.html.erb
目标是能够单击链接并开始下载将数据库对象保存在表中的 excel 或 csv 文件。
我第一次运行这段代码时它工作了,我的系统上仍然有下载的文件。但是,此后每次我都收到此错误:
Missing template products/index, application/index with {:locale=>[:en], :formats=>[:xls], :variants=>[], :handlers=>[:erb, :builder, :raw, :ruby, :coffee, :jbuilder]}. Searched in: * "/Users/Candied_Island/Desktop/CPF/export_excel_tutorial/app/views"
My index.hrml.erb is in the correct place, and I believe my partial is in the correct location as well. Please help if you can, I'm not seeing why I'm getting this error.
另外,如果它有帮助,它说我的错误发生在这里:
`app/controllers/products_controller.rb:4:in 'index'
这是这个代码块
respond_to do |format|
format.html
format.csv { send_data @products.to_csv }
format.xls
end
谢谢!