I am trying to build an advanced search using the Ransack gem in a Rails app. Here is some of my search form:
<%= search_form_for @q, url: search_listings_path,
html: { method: :post } do |f| %>
<%= f.label :name_cont, "Name contains" %>
<%= f.text_field :name_cont, placeholder:"e.g. price, name, location" %>
<%= f.label :price_gteq, "Minimum Price" %>
<%= f.number_field :price_gteq, placeholder: "0" %>
<%= f.label :price_lteq, "Maximum Price" %>
<%= f.number_field :price_lteq, placeholder: "0" %>
<%= f.check_box :basement %>
<%= f.label :basement_false, "No Basements" %>
Listing must have:
<%= f.check_box :wheelchair_accessible_true %>
<%= f.label :wheelchair_accessible_true, "Wheelchair Accessible" %>
<%= f.check_box :assistance_animal_true %>
<%= f.label :assistance_animal_true, "Assistance Animal Friendly" %>
<%= f.check_box :ac_true %>
<%= f.label :ac_true, "Central AC" %>
....
All of these "Listing must have" fields, (:wheelchair_accessibile, :assistance_animal, :ac, etc) are boolean columns on the Listings table. There are a quite a few more I'd like to be able to add and cross-reference for this search as well.
The name and price fields seem to work fine, but the check boxes are more dicey. If I only have one of these fields in the form, it works but as I add others, the search does not return listings that have both columns as "true". I think it's actually searching for unchecked boxes as false, when I just those fields ignored. Also "No basements" (looking for a 'false' value) doesn't seem to work at all. Any ideas?
Here is my controller:
def index
@q = Listing.ransack(params[:q])
if params[:q]
@listings = @q.result.distinct.page(params[:page]).per(10)
@q.build_condition if @q.conditions.empty?
@q.build_sort if @q.sorts.empty?
# @listings = Listing.near(params[:q])
elsif params[:latitude] && params[:longitude]
@listings = Listing.near([params[:latitude], params[:longitude]])
else
@listings = Listing.order("created_at DESC").page(params[:page]).per(10)
end
def search
index
render :index
end
Here is the server log when I search with only the box for "no basements" checked:
Started POST "/listings/search" for 127.0.0.1 at 2016-03-17 16:35:33 -0400
Started POST "/listings/search" for 127.0.0.1 at 2016-03-17 16:35:33 -0400
Processing by ListingsController#search as HTML
Processing by ListingsController#search as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Z9cLvHsTnO+UZNoWKzv9QFU8HpMYC/oVELIw5u4oXL2vYOAzs7 K5QCCHcXKbotDqWBspAcxtQrylE97l789A6Q==", "q"=>{"name_cont"=>"", "price_gteq"=>"", "price_lteq"=>"", "location_cont"=>"Toronto", "bedrooms_gteq"=>"", "basement"=>"1", "wheelchair_accessible_true"=>"0", "assistance_animal_true"=>"0", "ac_true"=>"0"}, "commit"=>"Search"}
Parameters: {"utf8"=>"✓", "authenticity_token"=>"Z9cLvHsTnO+UZNoWKzv9QFU8HpMYC/oVELIw5u4oXL2vYOAzs7 K5QCCHcXKbotDqWBspAcxtQrylE97l789A6Q==", "q"=>{"name_cont"=>"", "price_gteq"=>"", "price_lteq"=>"", "location_cont"=>"Toronto", "bedrooms_gteq"=>"", "basement"=>"1", "wheelchair_accessible_true"=>"0", "assistance_animal_true"=>"0", "ac_true"=>"0"}, "commit"=>"Search"}
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 39]]
User Load (0.4ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 39]]
(0.4ms) SELECT DISTINCT COUNT(DISTINCT "listings"."id") FROM "listings" WHERE ("listings"."location" ILIKE '%Toronto%' AND "listings"."wheelchair_accessible" != 't' AND "listings"."assistance_animal" != 't' AND "listings"."ac" != 't')
(0.4ms) SELECT DISTINCT COUNT(DISTINCT "listings"."id") FROM "listings" WHERE ("listings"."location" ILIKE '%Toronto%' AND "listings"."wheelchair_accessible" != 't' AND "listings"."assistance_animal" != 't' AND "listings"."ac" != 't')
Listing Load (0.6ms) SELECT DISTINCT "listings".* FROM "listings" WHERE ("listings"."location" ILIKE '%Toronto%' AND "listings"."wheelchair_accessible" != 't' AND "listings"."assistance_animal" != 't' AND "listings"."ac" != 't') LIMIT 10 OFFSET 0
Listing Load (0.6ms) SELECT DISTINCT "listings".* FROM "listings" WHERE ("listings"."location" ILIKE '%Toronto%' AND "listings"."wheelchair_accessible" != 't' AND "listings"."assistance_animal" != 't' AND "listings"."ac" != 't') LIMIT 10 OFFSET 0
Rendered collection (0.0ms)
Rendered collection (0.0ms)
Rendered listings/index.html.erb within layouts/application (8.3ms)
Rendered listings/index.html.erb within layouts/application (8.3ms)
Completed 200 OK in 54ms (Views: 42.3ms | ActiveRecord: 4.2ms)
Completed 200 OK in 54ms (Views: 42.3ms | ActiveRecord: 4.2ms)
Thank you!
Aucun commentaire:
Enregistrer un commentaire