Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue#669 bad page number parameters causing ArgumentError #1144

Merged
merged 6 commits into from
Jun 10, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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!
Expand Down Expand Up @@ -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
23 changes: 23 additions & 0 deletions spec/controllers/application_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -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