Reputation: 151
I've been creating a simple search form that would allow me to enter 3 different criteria in order to find a car in inventory. The three criteria are car VIN or car Model or car Color. What I have done so far only allows me to search for either a VIN number or a color but whenever I enter a model the search does not return anything.
Here is 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 is my model:
def self.search(search, car_VIN, car_model, car_color)
if search
where('car_VIN LIKE ? OR car_model LIKE ? OR car_color LIKE ?', "%#{search}%", "%#{search}%", "%#{search}%")
else
where(nil)
end
end
Here is my view:
<%= form_tag cars_path, :method => 'get' do %>
<p>
<%= text_field_tag :search, params[:search], :placeholder => "Car VIN, model or color" %>
<%= submit_tag "Search", :name => nil %>
</p>
I really can't pinpoint the cause of why it would not return items when entering a car model but it would if entering a VIN or a color. Help appreciated.
Upvotes: 2
Views: 778
Reputation: 151
Big facepalm... After spending an hour I realized that I was not entering vehicle models in the search form but vehicle makes therefore entering Toyota wouldn't return anything but entering Corolla would. Anyway since the model/controller/view I posted originally works feel free to use it as reference.
Upvotes: 1