feat: enhance city pagination and data fetching

- Update cities controller to include state in city data
- Modify results partial to pass pagination parameters
- Adjust index view to support turbo frame actions

These changes improve the pagination functionality by ensuring
that the correct parameters are passed for filtering cities
based on region, country, and state. This enhances the user
experience by providing more relevant data and better
navigation through the city listings.
This commit is contained in:
songtianlun 2025-02-26 15:31:45 +08:00
parent 070a3dfc1c
commit df7fbcd118
4 changed files with 26 additions and 11 deletions

View File

@ -4,7 +4,7 @@ class CitiesController < ApplicationController
def index
@regions = Region.order(:name)
@cities = City.includes(:country, country: :region).order(:name)
@cities = City.includes(:country, :state).order(:name)
@latest_arts = WeatherArt.includes(:city, :image_attachment).latest(1)
if params[:query].present?
@ -33,7 +33,15 @@ class CitiesController < ApplicationController
format.turbo_stream {
render turbo_stream: turbo_stream.update("cities_results",
partial: "cities/results",
locals: { cities: @cities }
locals: {
cities: @cities,
pagination_params: {
region: params[:region],
country: params[:country],
state: params[:state],
query: params[:query]
}
}
)
}
end

View File

@ -19,6 +19,12 @@
<%= render 'shared/pagination',
collection: cities,
collection_name: 'cities' %>
collection_name: 'cities',
pagination_params: {
region: params[:region],
country: params[:country],
state: params[:state],
query: params[:query]
} %>
<% end %>
</div>

View File

@ -152,7 +152,7 @@
</div>
</div>
<%= turbo_frame_tag "cities_results" do %>
<%= turbo_frame_tag "cities_results",data: { turbo_action: "advance" } do %>
<%= render "cities/results", cities: @cities %>
<% end %>

View File

@ -1,5 +1,6 @@
<%# app/views/shared/_pagination.html.erb %>
<% if collection.total_pages > 1 %>
<% pagination_params ||= {} %>
<div class="flex flex-col items-center mt-16 gap-6">
<!-- 页码信息 -->
<div class="text-base-content/70 font-light">
@ -11,7 +12,7 @@
<!-- 分页控件 -->
<div class="join shadow-lg">
<!-- 首页 -->
<%= link_to url_for(page: 1, region: params[:region], country: params[:country], sort: params[:sort]),
<%= link_to url_for(pagination_params.merge(page: 1)),
class: "join-item btn btn-sm #{collection.first_page? ? 'btn-disabled' : 'btn-ghost'}" do %>
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 19l-7-7 7-7m8 14l-7-7 7-7" />
@ -19,7 +20,7 @@
<% end %>
<!-- 上一页 -->
<%= link_to url_for(page: collection.prev_page || 1, region: params[:region], country: params[:country], sort: params[:sort]),
<%= link_to url_for(pagination_params.merge(page: collection.prev_page || 1)),
class: "join-item btn btn-sm #{collection.first_page? ? 'btn-disabled' : 'btn-ghost'}" do %>
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7" />
@ -32,7 +33,7 @@
<% end_page = [collection.total_pages, collection.current_page + page_window].min %>
<% if start_page > 1 %>
<%= link_to 1, url_for(page: 1, region: params[:region], country: params[:country], sort: params[:sort]),
<%= link_to 1, url_for(pagination_params.merge(page: 1)),
class: "join-item btn btn-sm btn-ghost hover:bg-primary/5" %>
<% if start_page > 2 %>
<button class="join-item btn btn-sm btn-ghost btn-disabled">...</button>
@ -45,7 +46,7 @@
<%= page %>
</button>
<% else %>
<%= link_to page, url_for(page: page, region: params[:region], country: params[:country], sort: params[:sort]),
<%= link_to page, url_for(pagination_params.merge(page: page)),
class: "join-item btn btn-sm btn-ghost hover:bg-primary/5" %>
<% end %>
<% end %>
@ -55,12 +56,12 @@
<button class="join-item btn btn-sm btn-ghost btn-disabled">...</button>
<% end %>
<%= link_to collection.total_pages,
url_for(page: collection.total_pages, region: params[:region], country: params[:country], sort: params[:sort]),
url_for(pagination_params.merge(page: collection.total_pages)),
class: "join-item btn btn-sm btn-ghost hover:bg-primary/5" %>
<% end %>
<!-- 下一页 -->
<%= link_to url_for(page: collection.next_page || collection.total_pages, region: params[:region], country: params[:country], sort: params[:sort]),
<%= link_to url_for(pagination_params.merge(page: collection.next_page || collection.total_pages)),
class: "join-item btn btn-sm #{collection.last_page? ? 'btn-disabled' : 'btn-ghost'}" do %>
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7" />
@ -68,7 +69,7 @@
<% end %>
<!-- 末页 -->
<%= link_to url_for(page: collection.total_pages, region: params[:region], country: params[:country], sort: params[:sort]),
<%= link_to url_for(pagination_params.merge(page: collection.total_pages)),
class: "join-item btn btn-sm #{collection.last_page? ? 'btn-disabled' : 'btn-ghost'}" do %>
<svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 5l7 7-7 7M5 5l7 7-7 7" />