Rails 4 simple search form with more than two parameters -
i've been creating simple search form allow me enter 3 different criteria in order find car in inventory. 3 criteria car vin or car model or car color. have done far allows me search either vin number or color whenever enter model search not return anything.
here controller:
def index #@cars = car.all @cars = car.search(params[:search], params[:car_vin], params[:car_model], params[:car_color]).page(params[:page])
end
here model:
def self.search(search, car_vin, car_model, car_color) if search where('car_vin ? or car_model ? or car_color ?', "%#{search}%", "%#{search}%", "%#{search}%") else where(nil) end
end
here view:
<%= form_tag cars_path, :method => 'get' %> <p> <%= text_field_tag :search, params[:search], :placeholder => "car vin, model or color" %> <%= submit_tag "search", :name => nil %> </p>
i can't pinpoint cause of why not return items when entering car model if entering vin or color. appreciated.
big facepalm... after spending hour realized not entering vehicle models in search form vehicle makes therefore entering toyota wouldn't return entering corolla would. anyway since model/controller/view posted works feel free use reference.