2012-03-23 61 views
0

我想使用Devise的控制器辅助方法signed_in?检查用户是否已经签名,如果是的话我想重定向到一个特定的页面。设计如何添加登录检查自定义会话控制器?

我试过这个,但signed_in?方法总是返回true,我该如何做这个工作?

class SessionsController < Devise::SessionsController 
    def new 
    redirect_to root_url 
    end 

    def create 
    if signed_in?(resource_name) 
     redirect_to where_-_want_to_url 
    else 
     resource = warden.authenticate!(:scope => resource_name, :recall => "#{controller_path}#failure") 

     return sign_in_and_redirect(resource_name, resource) 
    end 
    end 

    def sign_in_and_redirect(resource_or_scope, resource=nil) 
    scope = Devise::Mapping.find_scope!(resource_or_scope) 
    resource ||= resource_or_scope 
    sign_in(scope, resource) unless warden.user(scope) == resource 
    return render :json => {:success => true, :redirect => stored_location_for(scope) || after_sign_in_path_for(resource)} 
    end 

    def failure  
    return render :json => { :success => false } 
    end 

end 
+0

你想用这个做什么?你只是想显示一个不同的**主页**取决于用户是否登录? – Ashitaka 2012-03-23 20:22:46

+0

不,有一种情况是登录页面被删除,但实际上用户已经登录。所以我想做服装重定向 – Rn2dy 2012-03-23 20:24:00

回答

0

你想要住在帮手。

当用户招牌式,节省了用户的CURRENT_USER

self.current_user = user 

要检查用户是否在您签署可以使用

def signed_in? 
    !current_user.nil? 
end 

从迈克尔·哈特尔http://ruby.railstutorial.org/chapters/sign-in-sign-out#sec:current_user

编辑:

对不起,我刚刚阅读编辑。你想要Devise的。

def'signed_in?'如何?好像?

对于第一个实现,我建议您自己滚动以更好地理解,并让您知道所有组件的快速定制位置。接下来你可以实现其他的方法。

0

你试过if user_signed_in?

现在,我真的不知道你是什么努力来完成,但我不会这样定义,在我的控制器路由。我会做这样的事情:

# config/routes.rb 
authenticated :user do 
    root :to => where_I_want_to_url 
end 
root :to => 'welcome#index'