Skip to content

Commit

Permalink
Add Search UI
Browse files Browse the repository at this point in the history
  • Loading branch information
dbackeus committed May 3, 2023
1 parent 5916f3c commit e4c908d
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class SearchController < ApplicationController
def index
@results = Elasticsearch.search(params[:query])
end
end
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<nav style="font-size: 1.5rem;">
<%= link_to "Posts", posts_path %>
<%= link_to "Links", links_path %>
<%= link_to "Search", search_path %>
</nav>
<%= yield %>
</body>
Expand Down
16 changes: 16 additions & 0 deletions app/views/search/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h1>Search</h1>

<%= form_with url: search_path, method: :get do |form| %>
<%= form.label :query %>
<%= form.text_field :query, value: params[:query], autofocus: true %>
<%= form.submit "Search" %>
<% end %>
<% if @results.present? %>
<h2>Results</h2>
<ul>
<% @results.each do |result| %>
<li><%= link_to result.title, result %></li>
<% end %>
</ul>
<% end %>
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
Rails.application.routes.draw do
resources :links
resources :posts
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

get "search" => "search#index", as: :search

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

0 comments on commit e4c908d

Please sign in to comment.