我有这样的问题:我正在接受当前用户的报告,并且取决于从输入中获取的日期。
但是我需要将它们传递到带有 xls 扩展名的同一页面中,现在我遇到了这样的错误 -
undefined method each for nil:NilClass
所以这是我的控制器:
def index
if params[:report] && params[:report][:start_date] && params[:report][:end_date]
@start_date = params[:report][:start_date]
@end_date = params[:report][:end_date]
@financial_reports = current_user.financial_reports.where(:created_at => @start_date.to_date..@end_date.to_date)
respond_to do |format|
format.html # index.html.erb
format.xls
format.xml { render :xml => @financial_reports }
end
end
end
这是我的 index.xls.erb
<?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">Amount</Data></Cell>
<Cell><Data ss:Type="String">Currency</Data></Cell>
<Cell><Data ss:Type="String">Created at</Data></Cell>
</Row>
<% @financial_reports.each do |financial_report| %>
<Row>
<Cell><Data ss:Type="Number"><%= financial_report.amount %></Data></Cell>
<Cell><Data ss:Type="String"><%= financial_report.currency %></Data></Cell>
<Cell><Data ss:Type="String"><%= financial_report.created_at %></Data></Cell>
</Row>
<% end %>
</Table>
</Worksheet>
</Workbook>
我的日志:
Started GET "/financial_reports.xls" for 127.0.0.1 at 2012-08-09 16:52:30 +0300
Processing by FinancialReportsController#index as XLS
[1m[36mUser Load (0.0ms)[0m [1mSELECT `users`.* FROM `users` WHERE `users`.`id` = 67 LIMIT 1[0m
Rendered financial_reports/index.xls.erb (0.0ms)
Completed 500 Internal Server Error in 20ms
ActionView::Template::Error (undefined method `each' for nil:NilClass):
11: <Cell><Data ss:Type="String">Currency</Data></Cell>
12: <Cell><Data ss:Type="String">Created at</Data></Cell>
13: </Row>
14: <% @financial_reports.each do |financial_report| %>
15: <Row>
16: <Cell><Data ss:Type="Number"><%= financial_report.amount %></Data></Cell>
17: <Cell><Data ss:Type="String"><%= financial_report.currency %></Data></Cell>
app/views/financial_reports/index.xls.erb:14:in `_app_views_financial_reports_index_xls_erb__791914298_25744200'
Rendered C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.2/lib/action_dispatch/middleware/templates/rescues/_trace.erb (10.0ms)
Rendered C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.2/lib/action_dispatch/middleware/templates/rescues/_request_and_response.erb (0.0ms)
Rendered C:/Ruby193/lib/ruby/gems/1.9.1/gems/actionpack-3.2.2/lib/action_dispatch/middleware/templates/rescues/template_error.erb within rescues/layout (30.0ms)
编辑:如果我打电话给控制器
@financial_reports = current_user.financial_reports
它正在工作,但是当我添加 WHERE 条件时 - 出现错误。
那么我怎样才能传递到 xml 视图中呢?