Skip to content

Commit

Permalink
adding search button function in the index page to query serach term…
Browse files Browse the repository at this point in the history
…s against the Site Name,Fits_id and Subnet CIDR
  • Loading branch information
juddin927 committed Nov 20, 2023
1 parent 45a5b99 commit 66a9dfc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
11 changes: 11 additions & 0 deletions app/controllers/sites_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@ class SitesController < ApplicationController
def index
@sites = Site.order(:fits_id).all
@navigation_crumbs = [["Home", root_path]]
@sites = if params[:query].present?
# Site.where('name LIKE ? OR fits_id LIKE ?', "%#{params[:query]}%", "%#{params[:query]}%")
Site.where('name LIKE ? OR fits_id LIKE ? OR id IN (
SELECT sn.site_id
FROM subnets s
INNER JOIN shared_networks sn ON s.shared_network_id = sn.id
WHERE s.cidr_block LIKE ?
)', "%#{params[:query]}%", "%#{params[:query]}%", "%#{params[:query]}%")
else
Site.all
end
end

def show
Expand Down
14 changes: 13 additions & 1 deletion app/views/sites/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,19 @@
<%= link_to "Client classes", client_classes_path, class: "govuk-button govuk-button--secondary" %>

<h3 class="govuk-heading-m">Sites</h3>

<!-- Adding the search form -->
<div style="text-align: right;class="govuk-grid-column-one-half search_bar">
<%= form_tag(dhcp_path, method: :get, class: "govuk-form") do %>
<%= text_field_tag :query, params[:query], placeholder: "Search sites by FITS ID or name...", class: 'govuk-input govuk-input--width-10' %>
<%= submit_tag "Search", class: "govuk-button", "data-module" => "govuk-button" %>
<% end %>
</div>
<!-- <div style="text-align: right; margin-bottom: 10px;">-->
<%#= form_tag(dhcp_path, method: :get, class: "govuk-form") do %>
<%#= text_field_tag :query, params[:query], placeholder: "Search sites..." %>
<%#= submit_tag "Search", class: "govuk-button" %>
<%# end %>
<!-- </div>-->
<% if can? :create, Site %>
<%= link_to "Create a new site", new_site_path, class: "govuk-button" %>
<% end %>
Expand Down

0 comments on commit 66a9dfc

Please sign in to comment.