Skip to content

Commit 39d123c

Browse files
committed
Added Users Search Bar
1 parent acd5706 commit 39d123c

5 files changed

Lines changed: 27 additions & 12 deletions

File tree

app/controllers/orders_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def create
2525
Cart.destroy(session[:cart_id])
2626
session[:cart_id] = nil
2727
redirect_to orders_path
28-
byebug
28+
# byebug
2929
end
3030

3131
private

app/controllers/static_pages_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ def home
1010
@products = Product.search(params[:search]).order('created_at ASC').paginate(page: params[:page], per_page: 5)
1111
else
1212
@products = Product.all.order('created_at ASC').paginate(page: params[:page], per_page: 5)
13+
end
1314
end
14-
end
1515

1616
def products; end
1717

app/controllers/users_controller.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,21 @@ class UsersController < ApplicationController
44
before_action :admin_user, only: :destroy
55

66
def index
7-
@users = User.where(activated: true).paginate(page: params[:page], per_page: 6)
7+
# @users = User.where(activated: true).paginate(page: params[:page], per_page: 6)
8+
if params[:search]
9+
@users = User.search(params[:search]).order('created_at ASC').paginate(page: params[:page], per_page: 5)
10+
else
11+
@users = User.all.order('created_at ASC').paginate(page: params[:page], per_page: 5)
12+
end
813
end
914

1015
def show
1116
@user = User.find(params[:id])
1217
@microposts = @user.microposts.paginate(page: params[:page], per_page: 4)
1318

14-
#extra
19+
# extra
1520
@micropost = current_user.microposts.build
16-
@feed_items = @user.feed.paginate(page: params[:page] , :per_page => 6)
21+
@feed_items = @user.feed.paginate(page: params[:page], per_page: 6)
1722
redirect_to(root_url) && return unless @user.activated?
1823
end
1924

app/models/user.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ def feed
7979
Micropost.where("user_id = ?", id)
8080
end
8181

82-
private
82+
def self.search(search)
83+
where('name LIKE ?', "%#{search}%")
84+
end
8385

8486
private
8587

app/views/users/index.html.erb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
11
<% provide(:title, 'All users') %>
2-
<h1>All users (<%= @users.count %>)</h3></h1>
2+
<h1>All users (<%= @users.count %>)</h3>
3+
</h1>
34

4-
<%= will_paginate %>
5+
<div class="input-group" id="adv-search">
6+
<%= form_tag(users_path, :method => "get", id: "search-form") do %>
7+
<%= text_field_tag :search, params[:search], placeholder: "Search..." , class: "form-control" %>
8+
<%= submit_tag "Search" , class: "form_submit" , type: "button"%>
9+
<% end %>
10+
</div>
511

6-
<ul class="users">
7-
<%= render @users %>
8-
</ul>
12+
<%= will_paginate %>
913

10-
<%= will_paginate %>
14+
<ul class="users">
15+
<%= render @users %>
16+
</ul>
17+
18+
<%= will_paginate %>

0 commit comments

Comments
 (0)