Since Rails 5, the golden standard for redirecting the browser to the page that issued the request, has been ActionController::Redirecting#redirect_back. Unfortunately redirect_back doesn’t allow you to modify in any way the URL provided by referer header.

If you need to add query parameters or fragments, you have to write your own solution. This is the implementation I’m currently using:

class ApplicationController < ActionController::Base
  ...

  def redirect_to_back(options = {})
    uri = URI(request.referer)
    uri.query = options.delete(:params)&.to_query
    uri.fragment = options.delete(:anchor)

    redirect_to(uri.to_s, options)
  end
end