0

So I am using Devise for user authentication in my rails app. I have a setup that checks if a user is a member of a league. If he is just a member of 1 league he gets redirected to the user_league page if he has several leagues, he is directed to his/her league index so they can pick what league they want to administer. the 'after_sign_in_path_for' method looks like this

def after_sign_in_path_for( resource_or_scope )
    user_leagues = UserLeague.where( :user_id => current_user.id )
    if( user_leagues.count == 1 )
        user_league_url( user_leagues[0].league.id )
    else
        "/users/#{current_user.id}/leagues"
    end
  end

I installed the rails admin gem 'Active Admin' which by default uses devise for authentication too. Everytime I try to sign in to Active Admin after installation i get this error:

undefined method `admin_user_url' for #<ActiveAdmin::Devise::SessionsController:0x007f8be2f3e420>

Basically I traced it down to if I remove the function 'after_sign_in_path_for' above and restart my rails server the admin works fine (but i cannot log into my app, the way i want to) and if i put it back in and restart my server, I get the error from the admin page again. Whats am I doing wrong? How can i maintain the login flow of my app and still log into active admin?

1 Answer 1

1

Without having used ActiveAdmin, I'd guess you are over-riding the way they are using the redirect internally?

You could try wrapping the code in a block of current_user, so your code only fires if it's the user logged in:

def after_sign_in_path_for(resource_or_scope)
  if current_user?
    user_leagues = UserLeague.where( :user_id => current_user.id )
    if( user_leagues.count == 1 )
      user_league_url( user_leagues[0].league.id )
    else
      "/users/#{current_user.id}/leagues"
    end
  else
    super
  end
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.