Skip to content

Commit dafe0a9

Browse files
committed
Added Ransack gem and implemented basic search
1 parent eec3f3e commit dafe0a9

File tree

6 files changed

+3498
-4
lines changed

6 files changed

+3498
-4
lines changed

Gemfile

+1
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,6 @@ gem "ultrahook", "~> 0.1.5"
8787

8888
gem "execjs", "~> 2.7"
8989

90+
gem "ransack", github: "activerecord-hackery/ransack"
9091

9192
gem "will_paginate-bootstrap4", "~> 0.2.2"

Gemfile.lock

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
GIT
2+
remote: https://github.com/activerecord-hackery/ransack.git
3+
revision: 90dd40d4f3d24800a072e090cf86b86157c4b4b9
4+
specs:
5+
ransack (2.3.2)
6+
activerecord (>= 5.2.1)
7+
activesupport (>= 5.2.1)
8+
i18n
9+
110
GEM
211
remote: https://rubygems.org/
312
specs:
@@ -349,6 +358,7 @@ DEPENDENCIES
349358
pg (>= 0.18, < 2.0)
350359
puma (~> 3.11)
351360
rails (~> 5.2.4, >= 5.2.4.2)
361+
ransack!
352362
sass-rails (~> 5.0)
353363
selenium-webdriver
354364
spring

app/controllers/listings_controller.rb

+5-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ class ListingsController < ApplicationController
44
before_action :set_listing, only: [:show, :favourite]
55

66
def index()
7-
@listings = Listing.where(purchased: false).paginate(page: params[:page], per_page: 16).order("id DESC")
7+
#Set search condition to only listings that HAVE NOT been purchased
8+
@search = Listing.where(purchased: false).search(params[:q])
9+
10+
#Show listings that HAVE NOT been purchased
11+
@listings = @search.result.paginate(page: params[:page], per_page: 16).order("id DESC")
812
end
913

1014
def new()

app/views/listings/index.html.erb

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@
22
<div class="d-flex justify-content-end mb-5">
33
<%= link_to "Create new", new_listing_path, class: "btn btn-primary" %>
44
</div>
5-
6-
<%= render "shared/display_listings" %>
5+
6+
<%= search_form_for @search do |f| %>
7+
<%= f.label :title_or_description_or_brand_name_or_condition_name_or_category_name_cont, "Search: " %>
8+
<%= f.search_field :title_or_description_or_brand_name_or_condition_name_or_category_name_cont %>
9+
<%= f.submit %>
10+
<% end %>
11+
12+
<%= render "shared/display_listings" %>
713

814
<div class="container mt-5">
915
<%= will_paginate @listings, renderer: WillPaginate::ActionView::BootstrapLinkRenderer, list_classes: %w(pagination justify-content-center) %>

0 commit comments

Comments
 (0)