Ruby on Rails Redirecting


Example

Assuming the route:

resources :users, only: [:index]

You can redirect to a different URL using:

class UsersController
  def index
    redirect_to "http://stackoverflow.com/"
  end
end

You can go back to the previous page the user visited using:

redirect_to :back

Note that in Rails 5 the syntax for redirecting back is different:

redirect_back fallback_location: "http://stackoverflow.com/"

Which will try to redirect to the previous page and in case not possible (the browser is blocking the HTTP_REFERRER header), it will redirect to :fallback_location