diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index 9d25b37f..1ecc4c15 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -7,7 +7,7 @@ class TopicsController < ApplicationController def index @topics = Topic.approved.with_talks.order(name: :asc) @topics = @topics.where("lower(name) LIKE ?", "#{params[:letter].downcase}%") if params[:letter].present? - @pagy, @topics = pagy(@topics, limit: 100, page: params[:page]) + @pagy, @topics = pagy(@topics, limit: 100, page: page_number) end def show @@ -17,7 +17,7 @@ def show gearbox_extra: true, gearbox_limit: [12, 24, 48, 96], overflow: :empty_page, - page: params[:page]&.to_i || 1 + page: page_number ) end @@ -26,4 +26,10 @@ def set_user_favorites @user_favorite_talks_ids = Current.user.default_watch_list.talks.ids end + + private + + def page_number + [params[:page]&.to_i, 1].compact.max + end end diff --git a/test/controllers/topics_controller_test.rb b/test/controllers/topics_controller_test.rb index 042c2402..1594a3d5 100644 --- a/test/controllers/topics_controller_test.rb +++ b/test/controllers/topics_controller_test.rb @@ -22,6 +22,14 @@ class TopicsControllerTest < ActionDispatch::IntegrationTest assert_select "##{dom_id(@topic2)}", 0 end + test "should get index with invalid page number" do + get topics_url(page: "'") + assert_response :success + assert_select "h1", "Topics" + assert_select "##{dom_id(@topic1)} > span", "1" + assert_select "##{dom_id(@topic2)}", 0 + end + test "should get show" do get topic_url(@topic1) assert_response :success