Skip to content

Instantly share code, notes, and snippets.

@amhspencer
Created April 8, 2015 01:16
Show Gist options
  • Save amhspencer/7a1f81e1b625e7baea56 to your computer and use it in GitHub Desktop.
Save amhspencer/7a1f81e1b625e7baea56 to your computer and use it in GitHub Desktop.
Overly Complex "Create" Method
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
@armandofox
Copy link

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 separate Session#create_for_admin and Session#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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment