This is my first time implementing an advanced search. I'm emulating pet finder, but for dogs and want the search functionality to be similar to that of pet finders search for pets. My desired outcome is to have the user input their desired preferences as to what kind of dog (breed, age, gender, location, etc.) they are looking for and when they click on search, it redirects and renders their desired dog. I'm not getting an error at all just not getting the desired results.
Please let me know if you need additional information.
I want to add that if I take out the if statement in searchs_controller.rb and just have: @dogs = Dog.search(params[:location], params[:breed], params[:age], params[:gender]).all
then it gives me this error:
ActionView::Template::Error - PG::UndefinedFunction: ERROR: operator does not exist: integer ~~ unknown
LINE 1: ...n LIKE '%92603%' AND breed LIKE '%corgi%' AND age LIKE '%Bab...
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT "dogs".* FROM "dogs" WHERE (location LIKE '%92603%' AND breed LIKE '%corgi%' AND age LIKE '%Baby%' AND gender LIKE '%female%'):
app/controllers/searchs_controller.rb
class SearchsController < ApplicationController
def index
if params[:search]
@dogs = Dog.search(params[:location], params[:breed], params[:age], params[:gender]).all
else
@dogs = Dog.all.order("age ASC")
end
end
end
app/models/dog.rb
Before:
class Dog < ApplicationRecord
def self.search(location, breed, age, gender)
return all unless location.present? || breed.present? || age.present? || gender.present?
where(['location LIKE ? AND breed LIKE ? AND age LIKE ? AND gender LIKE ?', "%#{location}%", "%#{breed.downcase}%", "%#{age}%", "%#{gender. downcase}%"])
end
end
After:
def self.search(location, breed, age, gender)
return all unless location.present? || breed.present? || age.present? || gender.present?
where('location @@ ? AND breed @@ ? AND age = ? AND gender @@ ?', location, breed.downcase, age, gender.downcase)
end
app/views/searchs/index.html.erb
<div class="doge-info">
<% @dogs.each do |d| %>
<h2>Meet: <%= d.name %></h2>
<h4><%= d.name %> is a <%= d.breed %></h4>
<% if d.gender.titleize == "Female" %>
<h4>She is <%= d.age %> years old.</h4>
<% else %>
<h4>He is <%= d.age %> years old.</h4>
<% end %>
<% end %>
</div>
app/views/dogs/_form.html.erb
Updated 03/08:
<div class="search-for-dogs-form">
<%= form_tag searchs_index_path, method: "get", class: "search-dogs" do %>
<div id="form-searching-for-dogs">
<div class="location">
<% label_tag :location, class: "location-label" %>
<%= number_field_tag :location, params[:location], max: 0..5, placeholder: "Zip Code", class: "zip-code-area" %>
</div>
<div class="dog-breed">
<% label_tag :breed, class: "breed-label" %>
<%= text_field_tag :breed, params[:breed], placeholder: "Breed", class: "breed-text-field" %>
</div>
<div class="dog-age">
<p class="age-name">Choose an Age Range:</p>
<%= radio_button_tag :age, "#{0..2}" %>
<%= label_tag :age_baby, "0-2 years", class: "age-baby" %>
<%= radio_button_tag :age, "#{3..5}" %>
<%= label_tag :age_young, "3-5 years", class: "age-young" %>
<%= radio_button_tag :age, "#{6..8}" %>
<%= label_tag :age_adult, "6-8 years", class: "age-adult" %>
</div>
<div class="dog-gender">
<% label_tag :gender, class: "gender-label" %>
<%= text_field_tag :gender, params[:gender], placeholder: "Dog Gender", class: "gender-text-field" %>
</div>
<div class="submit-button-for-search">
<%= submit_tag "Search for Dogs", name: nil, class: "dog-search-submit-button" %>
</div>
</div>
<% end %>
</div>
app/config/routes.rb
devise_for :admins
root to: 'homepages#index'
resources :dogs, only: [:index, :show]
get "searchs/index"
namespace :admin do
resources :dogs
end
db/schema.rb
create_table "dogs", force: :cascade do |t|
t.string "name"
t.integer "age"
t.string "breed"
t.string "gender"
t.boolean "adoptable"
t.datetime "post_date"
t.string "color"
t.string "size"
t.string "birth_date"
t.string "photo"
t.string "location"
end