0

我有这样的问题:我正在接受当前用户的报告,并且取决于从输入中获取的日期。

但是我需要将它们传递到带有 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 视图中呢?

4

2 回答 2

0
undefined method each for nil:NilClass

意味着您正在处理一个nil 对象,在本例中是@financial_reports. 为什么?好吧,也许是因为您的数据库中没有符合您WHERE条件的记录。

在这种情况下,您应该检查 nil 对象而不是渲染 xls。

redirect_to <your_path_here> if @financial_reports.emtpy?
respond_to do |format|
....

调试此问题的最佳方法是在rails consoledevelopment.log. 我更喜欢development.log- 它会显示发送到数据库的 SQL 查询以及返回的内容。

更新 - 试试这个

改变:

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)

至:

if params[:start_date] && params[:end_date]
@financial_reports = current_user.financial_reports.where("financial_reports.created_at between ? and ?", params[:start_date].to_date, params[:end_date].to_date)
于 2012-08-09T13:48:14.543 回答
0

创建一个名为“app/views//index.xml.erb”的视图

并坚持你的观点(所有带有 erb 标签和东西的 XML 位)。

在您的控制器中

respond_to do |format|
  format.html
  format.xls
  format.xml
end


current_user.financial_reports.where("financial_reports.created_at between ? and ?",params[:start_date].to_date,params[:end_date].to_date)
于 2012-08-09T10:13:43.060 回答