'Rails 7 and Turbo

 # GET /rides or /rides.json
  def index
    @location = current_user.currently_at
    @location ||= 'Georgetown'
    @pagy, @rides = pagy(Ride.where(from: @location)
    .where(status: 'open')
    .where.not(id: current_user.driver.offers.select(:ride_id)).order(created_at: :asc), items: 7)
    @offer = Offer.new
  end

#ndex.html.erb
<div class="w-full px-2 mb-8">
  <% if notice.present? %>
    <p class="py-2 px-3 bg-green-50 mb-5 text-green-500 font-medium rounded-lg inline-block" id="notice"><%= notice %></p>
  <% end %>
  <h1 class="center-text text-xl">Rides in <%=@location%></h1>
  <span class="mb-2 inline-flex justify-left px-2 py-1 text-xs leading-none text-white bg-blue-700 rounded-lg "><%[email protected]%> rides available</span>
  <% if current_user.driver.status == 'working' %>
    <div>
      <h4>You currently have one job open. Complete it to see more jobs.
      </h4>
    </div>
  <% else %>
    <div id="locations" class="min-w-full">
      <% if @pagy.count > 7 %>
        <div class=" flex flex-col justify-center mx-4 text-xs text-gray-900 mt-1 mb-3 text-center">
          <%== pagy_info(@pagy) %>
          <div class="text-xs flex justify-center">
            <%== pagy_combo_nav_js(@pagy) %>
          </div>
        </div>
      <% end %>
      <%= render @rides %>
      <% if @pagy.count > 7 %>
        <div class=" flex flex-col justify-center mx-4 text-xs text-gray-900 mt-1 mb-16 text-center">
          <%== pagy_info(@pagy) %>
          <div class="text-xs flex justify-center">
            <%== pagy_combo_nav_js(@pagy) %>
          </div>
        </div>
      <% end %>
    </div>
  <% end %>
</div>

#_ride.html.erb
<div class = "pb-2 px-2 bg-white  rounded-2xl" id="<%= dom_id ride %>">
  <div class="flex flex-row-reverse ">
    <span class="mb-0 inline-flex justify-right px-2  mt-2 text-xs leading-none text-black bg-emerald-300 rounded-lg py-1 "><%=time_ago_in_words(ride.updated_at, include_seconds: true)%> ago</span>
  </div>
  <div class = "flex flex-col px-1">
    <p class = "my-block text-lg">
      <strong>Going to:</strong>
      <%= ride.to %>
    </p>
    <div class = "flex text-xs">
      <p class = "my-block text-xs  ">
        <strong>Pickup location:</strong>
        <%= ride.directions_from  %>
      </p>
      <p class = "my-block text-xs ml-2">
        <strong>Dropoff location:</strong>
        <%= ride.directions_to %>
      </p>
    </div>
    <% if action_name != "show" %>
      <div class="">
        <%= render '/offers/form', offer: @offer, ride: ride%>
      </div>
    <% end %>
  </div>
</div>
<br>

Given the code above, Id like to create a liveview such that anytime a ride from @rides in the index control changes, so does the view.

What would be the modifications necessary in the controller and view are "linked"

Conceptually I cant wrap my mind around how turbstreams tie together with the logic, views and partials



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source