Bret
Bret

Reputation: 23

Missing Template Error in Rails Exporting XLS files

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

Index file is in: export_excel/app/views/products/index.html.erb

Partial is in: export_excel/app/views/products/_product.html.erb

The objective is to be able to click a link and begin the download of a excel or csv file that holds database objects in a table.

The first time I ran this code it worked, and I still have the downloaded file on my stystem. However, everytime after that I've gotten this error:

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.

Also, if it helps any it says my error are happening here:

`app/controllers/products_controller.rb:4:in 'index'

which is this code block

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

Thanks!

Upvotes: 1

Views: 1896

Answers (2)

Rokibul Hasan
Rokibul Hasan

Reputation: 4156

You should have an file with .xls.erb file to render the request with xls formats. As your action the file should be index.xls.erb.

Here I add some example content for you according 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>

Upvotes: 1

ArtOfCode
ArtOfCode

Reputation: 5712

Rails is looking for a view, and not finding one. Specifically, it's looking for an XLS-format view that it can render, because you haven't specified any special action for the xls format like you have for csv.

You need to have an index.xls[.erb] view under app/views/products.

Your alternative is to do with XLS as you've done for 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

Upvotes: 1

Related Questions