Rails 导出 XLS 文件时缺少模板错误
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
索引文件位于:
export_excel/app/views/products/index.html.erb
部分在:
export_excel/app/views/products/_product.html.erb
objective 能够单击 link 并开始下载 excel 或 csv 文件,其中包含 table 中的数据库对象。
我第一次 运行 这段代码成功了,而且我的系统上仍然有下载的文件。但是,之后每次我都会收到此错误:
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
谢谢!
您应该有一个包含 .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>
Rails 正在寻找风景,但没有找到。具体来说,它正在寻找它可以呈现的 XLS 格式视图,因为您没有像 csv
.
那样为 xls
格式指定任何特殊操作
您需要在 app/views/products
下有一个 index.xls[.erb]
视图。
您的替代方案是像处理 CSV 一样处理 XLS:
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
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
objective 能够单击 link 并开始下载 excel 或 csv 文件,其中包含 table 中的数据库对象。
我第一次 运行 这段代码成功了,而且我的系统上仍然有下载的文件。但是,之后每次我都会收到此错误:
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
谢谢!
您应该有一个包含 .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>
Rails 正在寻找风景,但没有找到。具体来说,它正在寻找它可以呈现的 XLS 格式视图,因为您没有像 csv
.
xls
格式指定任何特殊操作
您需要在 app/views/products
下有一个 index.xls[.erb]
视图。
您的替代方案是像处理 CSV 一样处理 XLS:
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