Skip to content

Commit e93d3e9

Browse files
committed
Added Search Bar Functionality
1 parent ac5efbc commit e93d3e9

6 files changed

Lines changed: 42 additions & 52 deletions

File tree

app/controllers/line_items_controller.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ class LineItemsController < ApplicationController
22
before_action :logged_in_user, only: %i[create destroy add_quantity reduce_quantity]
33

44
def create
5+
# if(@order.nil?)
6+
# @order = Order.new(order_params)
7+
58
# Find associated product and current cart
69
chosen_product = Product.find(params[:product_id])
710
current_cart = @current_cart

app/controllers/orders_controller.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@ def create
2121
@order.line_items << item
2222
item.cart_id = nil
2323
end
24-
@order.save
24+
@order.save!
2525
Cart.destroy(session[:cart_id])
2626
session[:cart_id] = nil
2727
redirect_to orders_path
28+
byebug
2829
end
2930

3031
private

app/controllers/static_pages_controller.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,12 @@ def home
55
@feed_items = current_user.feed.paginate(page: params[:page], per_page: 6)
66
@current_user = current_user
77
end
8-
@products = Product.all.paginate(page: params[:page], per_page: 5)
8+
# @products = Product.all.paginate(page: params[:page], per_page: 5)
9+
if params[:search]
10+
@products = Product.search(params[:search]).order('created_at ASC').paginate(page: params[:page], per_page: 5)
11+
else
12+
@products = Product.all.order('created_at ASC').paginate(page: params[:page], per_page: 5)
13+
end
914
end
1015

1116
def products; end

app/models/product.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
class Product < ApplicationRecord
22
mount_uploader :picture, ProductPictureUploader
33
has_many :line_items, dependent: :destroy
4+
5+
def self.search(search)
6+
where('name LIKE ?', "%#{search}%")
7+
end
48
end

app/views/products/show.html.erb

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,7 @@
11
<p id="notice"><%= notice %></p>
22

33
<div class="center jumbotron">
4-
<p>
5-
<strong>Name:</strong>
6-
<%= @product.name %>
7-
</p>
8-
<p>
9-
<% if @product.picture? %>
10-
<%= image_tag @product.picture.url %>
11-
<% else %>
12-
<%= image_tag "no_image.png", size:"150x150" %>
13-
<%end%>
14-
</p>
15-
16-
<p>
17-
<strong>Description:</strong>
18-
<%= @product.description %>
19-
</p>
20-
21-
<p>
22-
<strong>Price:</strong>
23-
<%= @product.price %>
24-
$
25-
</p>
26-
27-
<!-- Add to Cart -->
28-
<%= link_to line_items_path(:product_id => @product.id) , class: "btn btn-warning" , style: "margin-top:15px;" do %>
29-
<i class="glyphicon glyphicon-plus" style="color:black;"></i>
30-
Add to Cart
31-
<% end %>
32-
33-
<p>
34-
<% if logged_in? && current_user.admin? %>
35-
<%= link_to 'Edit', edit_product_path(@product) %>
36-
|
37-
<%= link_to 'Back', products_path %>
38-
<% end %>
39-
</p>
40-
4+
<ul class="users">
5+
<%= render 'products/product' , :product => @product%>
6+
</ul>
417
</div>

app/views/shared/_products.html.erb

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
<!-- Search Bar -->
22
<div class="input-group" id="adv-search">
3-
<input type="text" class="form-control" placeholder="Search...."/>
3+
<%= form_tag(root_url, :method => "get", id: "search-form") do %>
4+
<%= text_field_tag :search, params[:search], placeholder: "Search..." %>
5+
<%= submit_tag "Search" %>
6+
<% end %>
7+
<!-- <input type="text" class="form-control" placeholder="Search...."/>
48
<div class="input-group-btn">
59
<div class="btn-group" role="group">
6-
<div class="dropdown dropdown-lg">
7-
<button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
10+
<div class="dropdown dropdown-lg"> -->
11+
<!-- <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
812
<span class="caret"></span></button>
913
<div class="dropdown-menu dropdown-menu-right" role="menu">
1014
<form class="form-horizontal" role="form">
@@ -28,12 +32,12 @@
2832
<button type="submit" class="btn btn-primary">
2933
<span class="glyphicon glyphicon-search" aria-hidden="true"></span></button>
3034
</form>
31-
</div>
32-
</div>
35+
</div> -->
36+
<!-- </div>
3337
<button type="button" class="btn btn-primary">
3438
<span class="glyphicon glyphicon-search" aria-hidden="true"></span></button>
3539
</div>
36-
</div>
40+
</div> -->
3741
</div>
3842

3943
<!-- Only Admin is allowed to create new products -->
@@ -49,11 +53,18 @@
4953
<%= will_paginate @products %>
5054

5155
<ul class="users">
52-
<%= render @products %>
53-
<%# @products.each do |product| %>
54-
<%#= render 'shared/product' , product: product %>
55-
<%# end %>
56-
</ul>
56+
<% if @products.present? %>
5757

58-
<%= will_paginate @products %>
59-
<br>
58+
<%= render @products %>
59+
<%# @products.each do |product| %>
60+
<%#= render 'shared/product' , product: product %>
61+
<%# end %>
62+
</ul>
63+
64+
<%= will_paginate @products %>
65+
66+
<% else %>
67+
<p>There are no posts containing the term(s)
68+
<%= params[:search] %>.</p>
69+
<% end %>
70+
<br>

0 commit comments

Comments
 (0)