Search code examples
ruby-on-railsrubyruby-on-rails-2

Error nil using paginate with conditions in the controller


I got this error "undefined method `paginate' for nil:NilClass"

Here is my table:

 policies
   id   num_policy   type_id    doc    name
    1        2323      1       181844  Charles
    2        2121      2       151511  Aguardientico
    3        3423      2       434344  Benjamin

Here is my controller:

if params[:num_policy].present?
  @search= Policy.find(:all,:conditions=>['num_policy LIKE ?',params[:num_policy] ])
end

if params[:type_id].present?
  @search= Policy.find(:all,:conditions=>['type_id LIKE ?',params[:type_id] ])
end

if params[:doc].present?
  @search= Policy.find(:all,:conditions=>['doc LIKE ?',params[:doc] ])
end

if params[:name].present?
  @search= Policy.find(:all,:conditions=>['name LIKE ?',params[:name] ])
end

if params[:doc].present? and params[:type_id].present?
  @search= Policy.find(:all,:conditions=>['doc LIKE ? and type_id LIKE ?',params[:doc],params[:type_id] ])
end

@policies= @search.paginate( :page => params[:page], :per_page =>2)

Here is my view

 <% @policies.each do |p| %>
   <%= p.num_policy %>
   <%= p.type_id %>
   <%= p.doc %>
   <%= p.name %>
 <% end %>

But if remove paginate I got this error "You have a nil object when you didn't expect it! You might have expected an instance of Array. The error occurred while evaluating nil.each"

 #controller
 @policies= @search

 #says that my line  <% @policies.each do |p| %>  is nil

My conditions seems not working with paginate

And also i tried

 <% @search.each do |p| %>
   <%= p.num_policy %>
   <%= p.type_id %>
   <%= p.doc %>
   <%= p.name %>
 <% end %>

I found this using conditions but don't know use it

        # define what are the search keys that can be found in params[]
        search_keys = [:a, :b, :c, :d, :e, :f]

        # extract hash values that concern only the search criteria
        search_params = params.select { |k,v| search_keys.include? k and ! v.nil? and ! v.empty? }

        # Write query according to search criteria presents
        case search_params.keys.sort
          when [:a]
            # Write the query when only :a search criteria is present
            Something.where("blahblah = ?", params[:a])
          when [:a, :c]
            # Write the query when only :a and :c search criteria are present
            Something.where("a_field = ? AND stuff = ?", params[:a], params[:c])
          when ...
            ...
        end

Please somebody can help me with this?

I will really appreciate help.


Solution

  • You are getting errors in both the cases, irrespective of whether you are using paginate or not.

    Seems like your @search is nil (didn't fall in any if statement) which initialized @policies with nil. Hence, it couldn't find paginate on NilClass.

    Update: Add the line

    @search = Policy.all if @search.nil? 
    

    or

    @search ||= Policy.all
    

    just above the line @policies= @search.paginate( :page => params[:page], :per_page =>2).

    That will verify this prediction.