instance method redirect_back_or_to

Ruby on Rails 7.1.6

Since v7.0.10

Available in: v7.0.10 v7.1.6 v7.2.3 v8.0.4 v8.1.2

Signature

redirect_back_or_to(fallback_location, allow_other_host: _allow_other_host, **options)

Redirects the browser to the page that issued the request (the referrer) if possible, otherwise redirects to the provided default fallback location.

The referrer information is pulled from the HTTP Referer (sic) header on the request. This is an optional header and its presence on the request is subject to browser security settings and user preferences. If the request is missing this header, the fallback_location will be used.

redirect_back_or_to({ action: "show", id: 5 })
redirect_back_or_to @post
redirect_back_or_to "http://www.rubyonrails.org"
redirect_back_or_to "/images/screenshot.jpg"
redirect_back_or_to posts_url
redirect_back_or_to proc { edit_post_url(@post) }
redirect_back_or_to '/', allow_other_host: false

Options

  • :allow_other_host - Allow or disallow redirection to the host that is different to the current host, defaults to true.

All other options that can be passed to #redirect_to are accepted as options, and the behavior is identical.

Parameters

fallback_location req
allow_other_host key = _allow_other_host
options keyrest
Source
# File actionpack/lib/action_controller/metal/redirecting.rb, line 127
    def redirect_back_or_to(fallback_location, allow_other_host: _allow_other_host, **options)
      if request.referer && (allow_other_host || _url_host_allowed?(request.referer))
        redirect_to request.referer, allow_other_host: allow_other_host, **options
      else
        # The method level `allow_other_host` doesn't apply in the fallback case, omit and let the `redirect_to` handling take over.
        redirect_to fallback_location, **options
      end
    end

Defined in actionpack/lib/action_controller/metal/redirecting.rb line 127 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActionController::Redirecting

Type at least 2 characters to search.

↑↓ navigate · open · esc close