【问题标题】:Missing Template Error in Rails Exporting XLS filesRails 导出 XLS 文件时缺少模板错误
【发布时间】:2017-04-26 17:34:09
【问题描述】:

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=&gt;[:en], :formats=&gt;[:xls], :variants=&gt;[], :handlers=&gt;[: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

谢谢!

【问题讨论】:

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


    【解决方案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>
    

    【讨论】:

    • 我认为您在那里粘贴了错误的示例内容 - 这适用于 .html.erb,但不应该在 XLS 文件中。
    • 没有 .xls 适用于 html 表,我已经发布了我的工作应用程序之一。
    • 工作,是的,但用户会期望 XLS 文件包含有效的 Excel 文件,而这...不是。
    • 其实这是一种使用对象和简单标记的简单方法,没有任何复杂性。
    • 你没有理解我的意思。您已将 HTML 放入用户期望的 Excel 文件中。
    【解决方案2】:

    Rails 正在寻找视图,但没有找到。具体来说,它正在寻找它可以呈现的 XLS 格式视图,因为您没有像为 csv 那样为 xls 格式指定任何特殊操作。

    您需要在app/views/products 下拥有index.xls[.erb] 视图。

    您的替代方法是使用 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
    

    【讨论】:

      猜你喜欢
      • 2015-09-08
      • 1970-01-01
      • 1970-01-01
      • 2017-12-24
      • 1970-01-01
      • 2015-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多