导出XLS文件的Rails中缺少模板错误

时间:2017-04-26 17:34:10

标签: ruby-on-rails ruby csv xls respond-to

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:'index'

这是代码块

respond_to do |format|
  format.html
  format.csv { send_data @products.to_csv }
  format.xls
end

谢谢!

2 个答案:

答案 0 :(得分:1)

Rails正在寻找一个视图,而不是找到一个视图。具体来说,它正在寻找可以呈现的XLS格式视图,因为您没有像~/.ipython/profile_default/startup/10-mystartupstuff.py那样为xls格式指定任何特殊操作。

您需要在csv下设置index.xls[.erb]视图。

您可以选择使用XLS,因为您已完成CSV:

app/views/products
class Product < ActiveRecord::Base
  def self.to_xls
    # get xls format data somehow and return it
  end
end

答案 1 :(得分:0)

您应该有一个包含.xls.erb文件的文件,以xls格式呈现请求。作为您的操作,文件应为index.xls.erb

我在此根据RailsCast#362

为您添加一些示例内容
    <?xml version="1.0"?>
    <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
              xmlns:o="urn:schemas-microsoft-com:office:office"
              xmlns:x="urn:schemas-microsoft-com:office:excel"
              xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
              xmlns:html="http://www.w3.org/TR/REC-html40">
      <Worksheet ss:Name="Sheet1">
        <Table>
          <Row>
            <Cell><Data ss:Type="String">ID</Data></Cell>
            <Cell><Data ss:Type="String">Name</Data></Cell>
            <Cell><Data ss:Type="String">Release Date</Data></Cell>
            <Cell><Data ss:Type="String">Price</Data></Cell>
          </Row>
          <% @products.each do |product| %>
              <Row>
                <Cell><Data ss:Type="Number"><%= product.id %></Data></Cell>
                <Cell><Data ss:Type="String"><%= product.name %></Data></Cell>
                <Cell><Data ss:Type="String"><%= product.released_on %></Data></Cell>
                <Cell><Data ss:Type="Number"><%= product.price %></Data></Cell>
              </Row>
          <% end %>
        </Table>
      </Worksheet>
    </Workbook>