Created
February 25, 2011 10:42
-
-
Save overture8/843627 to your computer and use it in GitHub Desktop.
Handy rails redirects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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