Müs*_*sli 3 devise ruby-on-rails-3
我正在使用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
Run Code Online (Sandbox Code Playgroud)
我的路线定义
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
Run Code Online (Sandbox Code Playgroud)
所以,当我用一个创建了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".
Run Code Online (Sandbox Code Playgroud)
出什么问题了?
redirect_to owner_company_addresses_path and return
Run Code Online (Sandbox Code Playgroud)
所以,我只想重定向到新的地址路径.我不明白为什么我得到错误.
提前致谢.
----编辑----
似乎只返回一个路径(我认为使用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? || !@addr_pending
@final_url = owner_company_addresses_path
end
end
end
@final_url
end
Run Code Online (Sandbox Code Playgroud)
Vas*_*ich 12
您应该删除redirect_to方法调用和return语句.after_sign_in_path_for应该只返回一条路径:
例如:
def after_sign_in_path_for(resource)
new_owner_company_path
end
Run Code Online (Sandbox Code Playgroud)