Skip to content

Instantly share code, notes, and snippets.

@overture8
Created February 25, 2011 10:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save overture8/843627 to your computer and use it in GitHub Desktop.
Save overture8/843627 to your computer and use it in GitHub Desktop.
Handy rails redirects
# Define in app controller
class ApplicationController < ActionController::Base
before_filter :require_login
# redirect somewhere that will eventually return back to here
def redirect_away(*params)
session[:original_uri] = request.request_uri
redirect_to(*params)
end
# returns the person to either the original url from a redirect_away or to a default url
def redirect_back(*params)
uri = session[:original_uri]
session[:original_uri] = nil
if uri
redirect_to uri
else
redirect_to(*params)
end
end
private
def require_login
redirect_away login_url unless self.authorized? # <-- REDIRECTING AWAY HERE (orig url saved in session)
end
end
class SessionsController < ApplicationController
def create
if user = User.authenticate(params[:email_or_user], params[:password])
self.current_user = user
redirect_back meetings_url # <--- REDIRECT_BACK HERE (to orig url)
else
flash[:error] = 'The login details you provided were not correct'
redirect_to(login_url)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment