redirect_back in Rails 5

redirect_back in Rails 5 is handy:

redirect_back(fallback_location: whatever_path)

fallback_location is used when HTTP_REFERER is not present.

In Rails 4 we had to use redirect_to :back and rescue RedirectBackError:

class MyController < ApplicationController
    rescue_from ActionController::RedirectBackError, with: :redirect_to_default

    def action
      redirect_to :back
    end

    private

    def redirect_to_default
      redirect_to root_path
    end
  end

						

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s