Ruby on Rails ActionController Redirecting

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

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



Got any Ruby on Rails Question?