1

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

谢谢!

4

2 回答 2

1

您应该有一个带有文件的.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>
于 2017-04-26T17:42:04.173 回答
1

Rails 正在寻找视图,但没有找到。具体来说,它正在寻找它可以呈现的 XLS 格式视图,因为您没有xls像为csv.

您需要index.xls[.erb]app/views/products.

您的替代方法是使用 XLS,就像对 CSV 所做的那样:

class Product < ActiveRecord::Base
  def self.to_xls
    # get xls format data somehow and return it
  end
end
class ProductsController < ApplicationController
  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_xls }
    end
  end
end
于 2017-04-26T17:42:37.080 回答