0
 # 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 "><%=@rides.count%> 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>

鉴于上面的代码,我想创建一个实时视图,以便在索引控件中来自@rides 的任何时间发生变化,视图也会发生变化。

控制器和视图中必要的修改是“链接”的

从概念上讲,我无法理解 turbstreams 如何与逻辑、视图和部分结合在一起

4

0 回答 0