Reputation: 1173
I have a rails 4 application that I'm trying to get to work with elasticsearch. Before I added the Elasticsearch, my code worked fine, but now I am getting this error:
undefined method `map' for nil:NilClass
My index view (I use haml):
= form_tag products_path, :method => :get do
= text_field_tag :query, params[:query]
= submit_tag "Search", :name => nil
= render "table"
and here is my _table partial:
- headers = @products.map(&:data).flat_map(&:keys).uniq
%table
%tr
- headers.each do |key|
%th= key
- @products.each do |product|
%tr
- headers.each do |key|
%td= product.data[key]
My ProductsController#index
def index
if params[:query].present?
Product.search(params[:query])
else
@products = Product.all.where(:product_type_id => @product.id)
end
end
My model:
class Product < ActiveRecord::Base
include Tire::Model::Search
include Tire::Model::Callbacks
belongs_to :product_type
end
Thanks!
Upvotes: 0
Views: 3562
Reputation: 5563
Looks like @products
will be nil if there is a query present. I think you meant to do something like this:
if params[:query].present?
@products = Product.search(params[:query])
else
@products = Product.all.where(:product_type_id => @product.id)
end
Upvotes: 1