Rails - How to integrate search results into table?

Viewed 63

I have this table

 t.string "first_name"
    t.string "last_name"
    t.integer "age"
    t.string "email"

I am using some search functionality where I search for a Barber by first_name . Then I am displaying it and whatever else is similar to the input.

This is my current view (search.html.erb)

<h1>searches</h1>


<%= @barbers.each do |barber|%>
<%= barber.first_name%><br>
<%end%>

Very plain and simple and this is how it looks.

enter image description here

I obviously don't want to keep it like this.

I want it to look like this , the way I have it in my index.html.erb

enter image description here

<table class="table table-dark table-striped">
  <thead>
    <tr>
      <th scope="row">First name</th>
      <th scope="row">Last name</th>
      <th scope="row">Age</th>
      <th scope="row">Email</th>
      <th scope="row">User ID</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @barbers.each do |barber| %>
      <tr>
        <td><%= barber.first_name %></td>
        <td><%= barber.last_name %></td>
        <td><%= barber.age %></td>
        <td><%= barber.email %></td>
        <td><%= barber.user_id %></td>
        <td><%= link_to 'Show', barber %></td>
        <td><%= link_to 'Edit', edit_barber_path(barber) %></td>
        <td><%= link_to 'Destroy', barber, method: :delete, data: { confirm: 'Are you sure?' } %></td>
      </tr>
    <% end %>
  </tbody>
</table>

I am just confused on how to write the code since I am searching for each. @barbers.each do |barber|

1 Answers

Here is what I done...

<h1>searches</h1>




<table class="table table-dark table-striped">
  <thead>
    <tr>
      <th scope="row">First name</th>
      <th scope="row">Last name</th>
      <th scope="row">Age</th>
      <th scope="row">Email</th>
      <th scope="row">User ID</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @barbers.each do |barber| %>
      <tr>
        <td><%= barber.first_name %></td>
        <td><%= barber.last_name %></td>
        <td><%= barber.age %></td>
        <td><%= barber.email %></td>
        <td><%= barber.user_id %></td>
        
      </tr>
    <% end %>
  </tbody>
</table>
Related