Created
April 8, 2015 01:16
-
-
Save amhspencer/7a1f81e1b625e7baea56 to your computer and use it in GitHub Desktop.
Overly Complex "Create" Method
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
class SessionsController < ApplicationController | |
def new | |
end | |
def create | |
@user = User.where(email: params[:session][:email].downcase).first | |
if @user && @user.authenticate(params[:session][:password]) | |
log_in @user | |
if session[:user_role] == :partner | |
redirect_to partner_path @user.id | |
elsif session[:user_role] == :admin | |
redirect_to admin_path @user.id | |
end | |
else | |
flash.now[:danger] = 'Invalid email/password combination' | |
render 'new' | |
end | |
end | |
def destroy | |
log_out | |
#redirect_to root_url | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm assuming that
session[:user_role]
is not known until after the auth step completes. (If it was known at the time the controller is called, I'd suggest having 2 separateSession#create_for_admin
andSession#create_for_partner
.)One possibility otherwise is to make a helper method that, for example, encapsulates line 7-14 but instead of doing a redirect it returns either a path to redirect to, or else nil (meaning authentication failed or whatever, and you should drop to line 15). How's that?