How can I search/filter by multiple parameters that are each optional in Rails 3.2? With the setup below, I am currently getting the following error. Any help is greatly appreciated.
undefined method `paginate' for nil:NilClass
Application Trace | Framework Trace | Full Trace
app/controllers/contacts_controller.rb:50:in `index'
Here is the index action in my contacts_controller:
def index
city = params[:city]
state = params[:state]
zip = params[:zip]
@contacts = Contact.search(city,state,zip).paginate(:page => params[:page], :per_page => items_per_page)
end
And here is the search method in my Contact model:
def self.search(city, state, zip)
joins(:profile => :addresses)
.where("city like ?", "%#{city}%") unless city.blank?
.where("state = ?", state) unless state.blank?
.where("zip like ?", "%#{zip}%") unless zip.blank?
end
Try to return more explicitely, e.g. like that: