diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 53e4b4b42..84198961b 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -11,7 +11,7 @@ class ApplicationController < ActionController::Base # Scrub sensitive parameters from your log # filter_parameter_logging :password - before_filter :set_header_variable, :set_view_path + before_filter :set_header_variable, :set_view_path, :validate_page_param before_action :configure_permitted_parameters, if: :devise_controller? def authenticate_active_admin_user! @@ -45,4 +45,14 @@ def ssl_required? def configure_permitted_parameters devise_parameter_sanitizer.permit(:sign_up, keys: [:name, :organisation]) end + + # this method is to respond to the will_paginate bug of invalid page number leading to error being thrown. + # see discussion here https://github.com/mislav/will_paginate/issues/271 + def validate_page_param + if params[:page].present? && params[:page].to_i > 0 + params[:page] = params[:page].to_i + else + params[:page] = nil + end + end end diff --git a/spec/controllers/application_controller_spec.rb b/spec/controllers/application_controller_spec.rb new file mode 100644 index 000000000..918bb9d6d --- /dev/null +++ b/spec/controllers/application_controller_spec.rb @@ -0,0 +1,23 @@ +require "spec_helper" + +describe ApplicationController do + controller do + def index + render text: nil + end + end + + describe "#validate_page_param" do + it "should try to convert page param to an integer" do + get :index, page: "2%5B&q" + + expect(controller.params[:page]).to eq(2) + end + + it "should default to page nil when no page number param is given" do + get :index, page: "%5B&q" + + expect(controller.params[:page]).to eq(nil) + end + end +end