Skip to content

Commit 331e45a

Browse files
committed
Add Search UI
1 parent 6d49b9a commit 331e45a

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class SearchController < ApplicationController
2+
def index
3+
@results = Searchable.search(params[:query])
4+
end
5+
end

app/views/layouts/application.html.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<nav style="font-size: 1.5rem;">
1515
<%= link_to "Posts", posts_path %>
1616
<%= link_to "Links", links_path %>
17+
<%= link_to "Search", search_path %>
1718
</nav>
1819
<%= yield %>
1920
</body>

app/views/search/index.html.erb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<h1>Search</h1>
2+
3+
<%= form_with url: search_path, method: :get do |form| %>
4+
<%= form.label :query %>
5+
<%= form.text_field :query, value: params[:query], autofocus: true %>
6+
<%= form.submit "Search" %>
7+
<% end %>
8+
9+
<% if @results.present? %>
10+
<h2>Results</h2>
11+
<ul>
12+
<% @results.each do |result| %>
13+
<li><%= link_to result.title, result %></li>
14+
<% end %>
15+
</ul>
16+
<% end %>

config/routes.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
Rails.application.routes.draw do
22
resources :links
33
resources :posts
4-
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
4+
5+
get "search" => "search#index", as: :search
56

67
# Reveal health status on /up that returns 200 if the app boots with no exceptions, otherwise 500.
78
# Can be used by load balancers and uptime monitors to verify that the app is live.

0 commit comments

Comments
 (0)