我正在使用 rails 3.2 和 Devise(最新版本)
主要思想是在登录后测试当前登录用户的一些变量。因此,例如,如果用户有待创建地址,我想重定向新地址路径。但我得到的是双重渲染错误。
这是代码
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
我的路线定义
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 和 return 就足够了,但事实并非如此)
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