Skip to content

Commit

Permalink
Refactor Search Form with SearchComponent
Browse files Browse the repository at this point in the history
This commit replaces the search form in `app/views/tags/index.html.erb` with the SearchComponent. The SearchComponent encapsulates the search form logic, making the code more readable and easier to change. It also takes care of HTML escaping the search_term, improving security. The form is now rendered inside a `<div>` instead of a `<span>` to follow HTML best practices.
  • Loading branch information
lucianghinda committed Sep 7, 2023
1 parent 6a69142 commit ebed20a
Showing 1 changed file with 4 additions and 22 deletions.
26 changes: 4 additions & 22 deletions app/views/tags/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,37 +1,19 @@
<% content_for :hero do %>
<%= render "shared/page_title", title: "What would you like to learn more about? ", description: "If you're looking to learn more about programming, we've got a few resources that might interest you. Whatever your interest, we're sure we can find a book or course that's perfect for you.", count: "#{Tag.count} topics" %>
<% end %>
<% content_for :content do %>
<% if params[:search_term].present? %>
<div class="pb-12">
<strong>Search Term: </strong><%= params[:search_term] %>
</div>
<% end %>

<div class="flex flex-wrap justify-between mb-8 xl:mb-16 gap-x-8 gap-y-2 md:gap-8">
<span>
<h2 class="h2 mb-8">All Topics</h2>
</span>

<span>
<%= form_tag(tags_path, method: "get", id: 'search_form', remote: true) do %>
<div class="input-group">
<span class="input-group-append">
<%= text_field_tag(:search_term, nil, placeholder: 'Search name', class: 'form-control', value: html_escape(params[:search_term])) %>
</span>
<span class="input-group-append">
<button type="submit" class="btn btn-primary button-rounded", id="submit-search-btn", title="Search Tags">Search</button>
<%= link_to tags_path, type: :submit, class: "btn btn-warning", id: "clear-search-btn", title: "Reset", style: "margin-left: 10px; color: black" do %> X <% end %>
</span>
</div>
<% end %>
<h2 class="mb-8 h2">All Topics</h2>
</span>
<div>
<%= render(SearchComponent.new(path: tags_path, search_term: params[:search_term])) %>
</div>
</div>

<%= render partial: "tags/list", locals: { list: @tags } %>
<% end %>




0 comments on commit ebed20a

Please sign in to comment.