2012-08-26 74 views
3

我與軌道3.2和設計(最新版本的)設計後登錄重定向 - 雙重渲染錯誤

主要的思想工作,如果測試登錄後當前登錄的用戶的一些變量因此,舉例來說。 ,如果用戶有待創建地址,我想重定向新的地址路徑。但是,我得到的是一個雙渲染錯誤。

下面是代碼

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? || [email protected]_pending 
        redirect_to owner_company_addresses_path and return 
       end 
      end 
     end 
     root_path 
    end 
end 

我的路線定義

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 

所以,當我與一個pedding地址創建一個用戶登錄我得到

"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". 

什麼是錯的用?

redirect_to owner_company_addresses_path and return 

所以,我只是想重定向到新的地址路徑。我不明白爲什麼我得到錯誤。

在此先感謝。

---- ----編輯

似乎只有一條路徑必須返回(我想使用redirect_to和回報已經足夠了,但事實並非如此)

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? || [email protected]_pending 
        @final_url = owner_company_addresses_path 
       end 
      end 
     end 
     @final_url 
    end 
+0

嘗試刪除'redirect_to'方法調用和返回語句。 'after_sign_in_path_for'應該只返回一個路徑。 –

+0

謝謝@nash。這是問題所在。你可以發表評論作爲答案,所以我可以接受它。 –

+0

很高興幫助。我已經發布了這個答案 –

回答

11

您應該刪除redirect_to方法調用和return聲明。 after_sign_in_path_for應該只返回一個路徑:

E.g:

def after_sign_in_path_for(resource) 
    new_owner_company_path 
end