Skip to content

Commit 257a9f1

Browse files
committed
Fixed search bugs
1 parent 1464247 commit 257a9f1

3 files changed

Lines changed: 69 additions & 66 deletions

File tree

app/models/product.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Product < ApplicationRecord
22
mount_uploader :picture, ProductPictureUploader
3-
has_many :line_items, dependent: :destroy
3+
# has_many :line_items, dependent: :destroy
44

55
def self.search(search)
66
where('name LIKE ?', "%#{search}%")
Lines changed: 42 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,56 @@
11
<!-- Search Bar -->
22
<div class="input-group" id="adv-search">
3-
<%= form_tag(root_url, :method => "get", id: "search-form") do %>
3+
<%= form_tag(root_url, :method => "get", id: "search-form") do %>
44
<%= text_field_tag :search, params[:search], placeholder: "Search..." , class: "form-control" %>
5-
<%= submit_tag "Search" , class: "form_submit" , type: "button"%>
6-
<% end %>
7-
<!-- <input type="text" class="form-control" placeholder="Search...."/>
8-
<div class="input-group-btn">
9-
<div class="btn-group" role="group">
10-
<div class="dropdown dropdown-lg"> -->
11-
<!-- <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
12-
<span class="caret"></span></button>
13-
<div class="dropdown-menu dropdown-menu-right" role="menu">
14-
<form class="form-horizontal" role="form">
15-
<div class="form-group">
16-
<label for="filter">Filter by</label>
17-
<select class="form-control">
18-
<option value="0">Featured</option>
19-
<option value="1">Most popular</option>
20-
<option value="2" selected="selected">Top rated</option>
21-
<option value="3">Most commented</option>
22-
</select>
23-
</div>
24-
<div class="form-group">
25-
<label for="contain">Top Seller</label>
26-
<input class="form-control" type="text"/>
27-
</div>
28-
<div class="form-group">
29-
<label for="contain">Contains the words</label>
30-
<input class="form-control" type="text"/>
31-
</div>
32-
<button type="submit" class="btn btn-primary">
33-
<span class="glyphicon glyphicon-search" aria-hidden="true"></span></button>
34-
</form>
35-
</div> -->
36-
<!-- </div>
37-
<button type="button" class="btn btn-primary">
38-
<span class="glyphicon glyphicon-search" aria-hidden="true"></span></button>
39-
</div>
40-
</div> -->
5+
<%= submit_tag "Search" , class: "form_submit"%>
6+
<% end %>
7+
<!-- <input type="text" class="form-control" placeholder="Search...."/> <div class="input-group-btn"> <div class="btn-group" role="group"> <div class="dropdown dropdown-lg"> -->
8+
<!-- <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-expanded="false"> <span class="caret"></span></button> <div class="dropdown-menu dropdown-menu-right" role="menu"> <form class="form-horizontal"
9+
role="form"> <div class="form-group"> <label for="filter">Filter by</label> <select class="form-control"> <option value="0">Featured</option> <option value="1">Most popular</option> <option value="2" selected="selected">Top rated</option> <option
10+
value="3">Most commented</option> </select> </div> <div class="form-group"> <label for="contain">Top Seller</label> <input class="form-control" type="text"/> </div> <div class="form-group"> <label for="contain">Contains the words</label> <input
11+
class="form-control" type="text"/> </div> <button type="submit" class="btn btn-primary"> <span class="glyphicon glyphicon-search" aria-hidden="true"></span></button> </form> </div> -->
12+
<!-- </div> <button type="button" class="btn btn-primary"> <span class="glyphicon glyphicon-search" aria-hidden="true"></span></button> </div> </div> -->
4113
</div>
4214

43-
<!-- Only Admin is allowed to create new products -->
44-
<% if logged_in? && current_user.admin? %>
15+
<%= will_paginate @products %>
16+
17+
<div>
18+
<!-- Only Admin is allowed to create new products -->
19+
<% if logged_in? && current_user.admin? %>
4520
<%= link_to new_product_path , class: "btn btn-success" , style: "margin-top:15px;" do %>
4621
<i class="glyphicon glyphicon-plus" style="color:black;"></i>
47-
Create New Product
48-
<%end %>
49-
<% end %>
22+
Create New Product (Admin)
23+
<%end %>
24+
<% end %>
5025

51-
<br/>
26+
<h2>
27+
<% if params[:search].nil? || params[:search].empty? %>
28+
All Products (<%= @products.count %>)
29+
<%else %>
30+
Found => [
31+
<%= pluralize(@products.count, " ] Products") %>
32+
<%end %>
33+
</h2>
34+
</div>
5235

53-
<%= will_paginate @products %>
5436

5537
<ul class="users">
56-
<% if @products.present? %>
38+
<% if @products.present? %>
5739

58-
<%= render @products %>
59-
<%# @products.each do |product| %>
60-
<%#= render 'shared/product' , product: product %>
61-
<%# end %>
62-
</ul>
40+
<%= render @products %>
41+
<%# @products.each do |product| %>
42+
<%#= render 'shared/product' , product: product %>
43+
<%# end %>
44+
</ul>
6345

64-
<%= will_paginate @products %>
46+
<%= will_paginate @products %>
6547

66-
<% else %>
67-
<p>There are no posts containing the term(s)
68-
<%= params[:search] %>.</p>
69-
<% end %>
70-
<br>
48+
<% else %>
49+
<p>
50+
<br /><br /><hr />
51+
There are no products containing the term(s)
52+
=> [ <strong><%= params[:search] %></strong> ] .
53+
54+
</p>
55+
<% end %>
56+
<br>

app/views/users/index.html.erb

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
11
<% provide(:title, 'All users') %>
2-
<h1>All users (<%= @users.count %>)</h1>
32

4-
<div class="input-group" id="adv-search">
5-
<%= form_tag(users_path, :method => "get", id: "search-form") do %>
6-
<%= text_field_tag :search, params[:search], placeholder: "Search by user name..." , class: "form-control" %>
7-
<%= submit_tag "Search" , class: "form_submit" , type: "button"%>
8-
<% end %>
3+
<h1>
4+
<% if params[:search].nil? || params[:search].empty? %>
5+
All users (<%= @users.count %>)
6+
<%else %>
7+
Found => [
8+
<%= pluralize(@users.count, " ] user") %>
9+
<%end %>
10+
</h1>
11+
12+
<div class="input-group" id="adv-search">
13+
<%= form_tag(users_path, :method => "get", id: "search-form") do %>
14+
<%= text_field_tag :search, params[:search], placeholder: "Search by user name..." , class: "form-control" %>
15+
<%= submit_tag "Search" , class: "form_submit"%>
16+
<% end %>
917
</div>
1018

1119
<%= will_paginate %>
1220

13-
<ul class="users">
14-
<%= render @users %>
15-
</ul>
21+
<% if @users.present? %>
22+
<ul class="users">
23+
<%= render @users %>
24+
</ul>
25+
<%else %>
26+
<p>
27+
<br/><br/><hr/>
28+
There are no users containing the term(s) => [
29+
<strong><%= params[:search] %></strong>
30+
] .
31+
</p>
32+
<%end %>
1633

1734
<%= will_paginate %>

0 commit comments

Comments
 (0)