Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Devise after login redirect - double render error

I'm working with rails 3.2 and Devise (the lastest version)

The main idea if the test some variables of the current logged user after sign in. So, for example, if the user has pending creating an address i want to redirect the new address path. But what i get is a double render error.

Here is the code

class ApplicationController < ActionController::Base
  protect_from_forgery

 # Devise: Where to redirect users once they have logged in
  def after_sign_in_path_for(resource)

        if current_user.is? :company_owner
            if $redis.hget(USER_COMPANY_KEY, current_user.id).nil?
                redirect_to new_owner_company_path and return
            else
                @addr_pending = $redis.hget(PENDING_ADDRESS_KEY,current_user.id)
                unless @addr_pending.nil? || !@addr_pending
                     redirect_to owner_company_addresses_path  and return
                end
            end
        end
        root_path
  end
end

my routes definition

  root :to => "home#index"
    devise_for :users, :controllers => { 
    :omniauth_callbacks => "users/omniauth_callbacks" 
  }
    resources :users, :only => :show

    namespace :owner do
        resource :company  do # single resource /owner/company
            get 'thanks'
            get 'owner' #TODO: esto hay que sacarlo de aquí y forme parte del login
            resources :addresses
        end
    end

So, when i login with a user with a pedding address creation i get

"render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".

what is wrong with?

 redirect_to owner_company_addresses_path  and return

So, i just want to redirect to the new address path. I don't understand why i get the error.

Thanks in advance.

---- edit ----

Seems that only one path must be returned (I thought with redirect_to and return was enough, but it does not)

def after_sign_in_path_for(resource)

        @final_url = root_path
        if current_user.is? :company_owner
            if $redis.hget(USER_COMPANY_KEY, current_user.id).nil?
                @final_url = new_owner_company_path
            else
                @addr_pending = $redis.hget(PENDING_ADDRESS_KEY,current_user.id)
                unless @addr_pending.nil? || !@addr_pending
                     @final_url = owner_company_addresses_path
                end
            end
        end
        @final_url
  end
like image 663
Müsli Avatar asked Dec 12 '22 23:12

Müsli


1 Answers

You should remove redirect_to method call and return statement. after_sign_in_path_for should return only a path:

E.g:

def after_sign_in_path_for(resource)
  new_owner_company_path
end
like image 63
Vasiliy Ermolovich Avatar answered Jan 04 '23 20:01

Vasiliy Ermolovich