diff --git a/app/controllers/cities_controller.rb b/app/controllers/cities_controller.rb index 1fb8aa8..504c0f5 100644 --- a/app/controllers/cities_controller.rb +++ b/app/controllers/cities_controller.rb @@ -1,6 +1,5 @@ class CitiesController < ApplicationController def index - @cities = City.all.order(:name) @regions = Region.includes(:countries).order(:name) @cities = City.includes(:country, country: :region).active.order(:name) @@ -13,6 +12,8 @@ class CitiesController < ApplicationController @current_country = Country.friendly.find(params[:country]) @cities = @cities.by_country(@current_country.id) end + + @cities = @cities.page(params[:page]).per(10) end def show diff --git a/app/views/arts/index.html.erb b/app/views/arts/index.html.erb index ddb77f4..7c677a9 100644 --- a/app/views/arts/index.html.erb +++ b/app/views/arts/index.html.erb @@ -101,87 +101,72 @@ - -
-
- <% @weather_arts.each do |art| %> -
- -
- <% if art.image.attached? %> - <%= image_tag art.image, - class: "w-full h-full object-cover transform group-hover:scale-105 transition-transform duration-500" %> +
+
+ <% @weather_arts.each do |art| %> +
+
+ <% if art.image.attached? %> + <%= image_tag art.image, + class: "w-full h-full object-cover transform group-hover:scale-105 transition-transform duration-500" %> - -
+
- -
-
-

+
+
+

+ <%= art.city.name %> +

+

+ <%= art.city.country.name %> +

+
+ + + + <%= art.description %> +
+
+
+ <% end %> +

+ + +
+
+
+

<%= art.city.name %>

-

- <%= art.city.country.name %> +

+ <%= art.weather_date.strftime("%B %d, %Y") %>

-
- - - - <%= art.description %> +
+
+
+ <%= art.temperature %>°C +
+
+ <%= art.humidity %>% humidity
- <% end %> -
- -
-
-
-

- <%= art.city.name %> -

-

- <%= art.weather_date.strftime("%B %d, %Y") %> -

-
-
-
- <%= art.temperature %>°C -
-
- <%= art.humidity %>% humidity -
-
+ <%= link_to city_weather_art_path(art.city, art), + class: "btn btn-primary btn-sm w-full" do %> + View Details + + + + <% end %>
- - <%= link_to city_weather_art_path(art.city, art), - class: "btn btn-primary btn-sm w-full" do %> - View Details - - - - <% end %>
-
- <% end %> -
- - - <% if @weather_arts.total_pages > 1 %> -
-
- <%= link_to_prev_page @weather_arts, "Previous", - class: "btn btn-outline #{'btn-disabled' unless @weather_arts.prev_page}" %> - <% @weather_arts.total_pages.times do |i| %> - <%= link_to i + 1, arts_path(page: i + 1, region: params[:region], sort: params[:sort]), - class: "btn btn-outline #{'btn-active' if @weather_arts.current_page == i + 1}" %> - <% end %> - <%= link_to_next_page @weather_arts, "Next", - class: "btn btn-outline #{'btn-disabled' unless @weather_arts.next_page}" %> -
+ <% end %>
- <% end %> + + <%= render 'shared/pagination', + collection: @weather_arts, + collection_name: 'weather arts' %> +
\ No newline at end of file diff --git a/app/views/cities/index.html.erb b/app/views/cities/index.html.erb index ebabcd8..a5c5bf7 100644 --- a/app/views/cities/index.html.erb +++ b/app/views/cities/index.html.erb @@ -12,7 +12,6 @@ <% end %> -
@@ -38,13 +37,10 @@
-
-
- - <% if @current_region %> -
<%= @cities.count %> <%= 'city'.pluralize(@cities.count) %> <% if @current_country %> @@ -117,10 +111,16 @@
-
-
- <%= render partial: 'city', collection: @cities %> +
+
+ <%= render partial: 'city', collection: @cities %> +
+ + <%= render 'shared/pagination', + collection: @cities, + collection_name: 'cities' %>
+
\ No newline at end of file diff --git a/app/views/shared/_pagination.html.erb b/app/views/shared/_pagination.html.erb new file mode 100644 index 0000000..3850813 --- /dev/null +++ b/app/views/shared/_pagination.html.erb @@ -0,0 +1,86 @@ +<%# app/views/shared/_pagination.html.erb %> +<% if collection.total_pages > 1 %> +
+ +
+ + Page <%= collection.current_page %> of <%= collection.total_pages %> + +
+ + +
+ + <%= link_to url_for(page: 1, region: params[:region], country: params[:country], sort: params[:sort]), + class: "join-item btn #{collection.first_page? ? 'btn-disabled' : 'btn-ghost'}" do %> + + + + <% end %> + + + <%= link_to url_for(page: collection.prev_page || 1, region: params[:region], country: params[:country], sort: params[:sort]), + class: "join-item btn #{collection.first_page? ? 'btn-disabled' : 'btn-ghost'}" do %> + + + + <% end %> + + + <% page_window = 2 # 当前页面前后显示的页码数 %> + <% start_page = [1, collection.current_page - page_window].max %> + <% 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]), + class: "join-item btn btn-ghost hover:bg-primary/5" %> + <% if start_page > 2 %> + + <% end %> + <% end %> + + <% (start_page..end_page).each do |page| %> + <% if page == collection.current_page %> + + <% else %> + <%= link_to page, url_for(page: page, region: params[:region], country: params[:country], sort: params[:sort]), + class: "join-item btn btn-ghost hover:bg-primary/5" %> + <% end %> + <% end %> + + <% if end_page < collection.total_pages %> + <% if end_page < collection.total_pages - 1 %> + + <% end %> + <%= link_to collection.total_pages, + url_for(page: collection.total_pages, region: params[:region], country: params[:country], sort: params[:sort]), + class: "join-item btn 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]), + class: "join-item btn #{collection.last_page? ? 'btn-disabled' : 'btn-ghost'}" do %> + + + + <% end %> + + + <%= link_to url_for(page: collection.total_pages, region: params[:region], country: params[:country], sort: params[:sort]), + class: "join-item btn #{collection.last_page? ? 'btn-disabled' : 'btn-ghost'}" do %> + + + + <% end %> +
+ + +
+ Showing <%= collection.offset_value + 1 %> to + <%= collection.last_page? ? collection.total_count : collection.offset_value + collection.limit_value %> + of <%= collection.total_count %> <%= collection_name || 'items' %> +
+
+<% end %> \ No newline at end of file