Skip to content

Commit

Permalink
Refactor Search Form in Newsletters with SearchComponent
Browse files Browse the repository at this point in the history
Description:
This commit replaces the search form in app/views/newsletters/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 3c05cde commit 9d06d45
Showing 1 changed file with 6 additions and 20 deletions.
26 changes: 6 additions & 20 deletions app/views/newsletters/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,33 +1,19 @@
<% content_for :hero do %>
<%= render "shared/page_title", title: "Newsletters about Ruby and Ruby on Rails", description: "Get the latest news, tips, and tricks delivered right to your inbox. Read about interesting articles and blog posts about life and programming.", count: "#{Newsletter.count} newsletters" %>
<% 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 Newsletters</h2>
</span>

<span>
<%= form_tag(newsletters_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 Newletter">Search</button>
<%= link_to newsletters_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 %>
</span>
<div>
<h2 class="mb-8 h2">All Newsletters</h2>
</div>
<div>
<%= render SearchComponent.new(path: newsletters_path, search_term: params[:search_term]) %>
</div>
</div>

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

0 comments on commit 9d06d45

Please sign in to comment.