未定义的方法`paginate'为nil:搜索时为NilClass

时间:2014-12-07 17:30:57

标签: ruby-on-rails-3 search activerecord rails-activerecord

我在Rails 3.2.20应用程序中做了一些基本的工作,我跟踪LostCalls。我有一切设置和工作,但在尝试实现搜索方法时,我的控制器在我尝试分页时抛出一个NilClass。

这是我的模型,视图和控制器的样子。

lostcalls_controller.rb

def index    
     @lostcalls = LostCall.report(params[:search])
     respond_to do |format|
         format.html do
           @lostcalls = @lostcalls.paginate(:per_page => params[:per_page] || 1, :page => params[:page]).order('call_date ASC')
         end
         format.csv { send_data LostCall.to_csv(@lostcalls) }
         format.pdf do
          pdf = LostCallPdf.new(@lostcalls)
          send_data pdf.render, filename: "lostcalls",
                                type: "application/pdf",
                                disposition: "inline"
         end
       end
   end

lost_call.rb模型

class LostCall < ActiveRecord::Base
  attr_accessible :call_date, :call_details, :entered_by, :facility_id
  belongs_to :facility

  scope :by_facility, lambda { |id| where(facility_id: id) }
  scope :search_between, lambda { |start_date, end_date| where("call_date BETWEEN ? AND ?", start_date.beginning_of_day, end_date.end_of_day)}
  scope :search_by_start_date,  lambda { |start_date| where('call_date BETWEEN ? AND ?', start_date.beginning_of_day, start_date.end_of_day) }
  scope :search_by_end_date, lambda { |end_date| where('call_date BETWEEN ? AND ?', end_date.beginning_of_day, end_date.end_of_day) }

  def self.report(search)
    search ||= { type: "all" }

    results = results.by_facility(search[:facility_name]) if search[:facility_name].present?

    if search[:start_date].present? && search[:end_date].present?
      results = results.search_between(Date.parse(search[:start_date]), Date.parse(search[:end_date]))
    else
      results = results.search_by_start_date(Date.parse(search[:start_date])) if search[:start_date].present?
      results = results.search_by_end_date(Date.parse(search[:end_date])) if search[:end_date].present?
    end
    results
  end
end

index.html.erb

<h3>Lost Call Log</h3>
<%= render 'search' %>
<%= render 'results' %>

_search.html.erb

<div>
  <%= form_tag lostcalls_path, :method => 'get' do %>  
    <p>
        <%= text_field_tag "search[start_date]", params[:search].try(:[], :start_date), :placeholder => 'Start Date', :class => 'input-large search-query ', id: 'start_date_select' %>
        to 
        <%= text_field_tag "search[end_date]", params[:search].try(:[], :end_date), :placeholder => 'End Date', :class => 'input-large search-query', id: 'end_date_select'   %>
      </p>

      <p>
        <%= select_tag "search[facility_name]", options_from_collection_for_select(Facility.order(:facility_name), :id, :facility_name, selected: params[:search].try(:[], :facility_name)), prompt: "Any Facility" %>   
      </p> 

      <p>
        Results Per Page
        <%= select_tag "per_page", options_for_select([["10", 10], ["25", 25] , ["50", 50], ["100", 100], ["All", 100000]], selected: params[:per_page]), class: "span1" %>
        <%= submit_tag "Search", :name => nil, :class => 'btn' %> <%= link_to "Export to CSV", lostcalls_path(params.merge(format: "csv")), :class => "btn btn-info" %> <%= link_to "Export To PDF", lostcalls_path(params.merge(format: "pdf")) , :class => 'btn btn-warning' %> <%= link_to "New Entry", new_lostcall_path, class: 'btn btn-danger' %>

      </p>
  <% end %>
</div>

_results.html.erb

<table class="table table-striped">
  <thead>
    <tr>
      <th>Call Date</th>
      <th>Facility</th>
      <th>Call Details</th>
      <th>Entered By</th>
      <th></th>
    </tr>
  </thead>

  <tbody>
    <% @lostcalls.each do |l| %>
      <tr>
        <td><%= l.call_date.strftime("%m/%d/%y") %></td>
        <td><%= l.facility.try(:facility_name) %></td>
        <td><%= l.call_details %></td>
        <td><%= l.entered_by %></td>
        <td><%= link_to 'View', lostcall_path(l), :class => 'btn btn-info btn-mini'%></td>
      </tr>
    <% end %>
  </tbody>
</table>
<%= will_paginate @lostcalls, :renderer => BootstrapPagination::Rails %>

我在另一个报告控制器中使用类似的功能来跟踪实际的呼叫(不是LostCalls),它工作正常。我认为我的报告类方法存在问题,但即使错误更好,我也很难调试它。

总而言之,如果我摆脱LostCall.report(params[:search])并执行`LostCall.order(&#34; call_date ASC&#34;),索引视图将起作用,它将正确分页。似乎报告方法已被破坏并在模型中返回nil。

如果我需要重构我的问题或提供其他信息,请告诉我们。我感谢您提供的任何帮助。

谢谢,美好的一天!

更新

感谢您的反馈,我忘了初始化结果变量。所以一个简单的results = scoped修复了它。 :)

2 个答案:

答案 0 :(得分:0)

看起来结果变量未在报表方法中初始化?

答案 1 :(得分:0)

每当您尝试访问索引页时,您的报告方法都将返回nil,因为它不会发送您在报告方法中使用的任何参数。
假设如果params [:start_date],params [:end_date]和params [:facility_name]名称不存在则报告方法将返回nil。

修复报表方法或从表单中的params发送默认值。您也可以在索引操作中添加条件来修复此问题。

由于您没有使用它,因此不需要第一行报告方法,因为您已在每一行添加了条件。