如何以其他格式渲染时传递params哈希?

时间:2012-08-09 09:46:32

标签: ruby-on-rails render params

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

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

2 个答案:

答案 0 :(得分:0)

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

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

在您的控制器中

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)

答案 1 :(得分: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)