0

I'm just trying to get search functionality working with form_with and Turbo, however I seem to be failing at the first hurdle. I know where I've got to isn't fully Turbo charged, but my understanding is at this point when a name is searched it should at least return that item.

I have a model called Player containing information (first_name, second_name etc). I have pulled the data I want into my home page and formatted in a table.

To start, I understand I need to move this to a partial. In _players.html.erb I've moved half the table (that contains the data) from the homepage:

static_pages/_players.html.erb

<% turbo_frame_tag "players" do %>
<% @players.each do |player| %>
    <% if  player.status == 'a' %>
    <tr>
    <td>
        <div class="flex items-center">
            <div class="block">
                <div><%= player.first_name %> <%= player.second_name %> <%= player.position %></div>
                <div><%= player.team %></div>
            </div>
        </div>
    </td>
    <td>
     <div>$<%= player.current_price / 10.to_f %></div>
    </td>
    <td>
     <div><%= player.average_points %></div>
    </td>
    <td>
     <div><%= player.total_points %></div>
    </td>
    <td>
     <div><%= player.form %></div>
    </td>
    <td>
     <div><%= player.age %></div>
    </td>
    </tr>
    <% end %>
<% end %>
<% end %>

In static_pages/home.html.erb. I have the form and rendered partial:

<%= form_with(url: players_path, method: :get, data: {turbo_frame: "players"}) do |form| 
   %>
  <%= form.label :query, "Search by player name", class:"block mb=2" %>
  <%= form.text_field :query, class: "rounded border ring-0"%>
  <%= form.submit "Search", class: "rounded border ring-0" %>
<% end %>

<table class="table-auto w-full ">
  <thead>
      <tr>
      <th>
        <div>Player</div>
      </th>
      <th>
        <div>Price</div>
      </th>
      <th>
        <div>Average Points</div>
      </th>
      <th>
        <div>Total Points</div>
      </th>
      <th>
        <div>Form</div>
      </th>
      <th">
        <div>Cost/Million</div>
      </th>
      </tr>
  </thead>

  <tbody>
    <%= render "players" %>
  </tbody>
</table>

In static_pages_controller:

class StaticPagesController < ApplicationController
def home
 if params[:query].present?
   @players = Player.where("name LIKE ?", "#{params[:query]}%")
 else
   @players = Player.all
 end
end
end

Currently the table in home.html.erb isn't showing. I appreciate this isn't fully turb charged yet but to just get it working would be great. Thanks in advance!

ldthompson
  • 29
  • 6

0 Answers0