2009-11-09 29 views
0

我刚刚完成的设置我的应用程序authlogic以下this工作究竟帮助,authlogic

开始之前我已经创建了一个产品目录与基本的CRUD功能。

现在我希望产品目录只有在用户已经登录时才可以访问。所以,基本上如果用户没有登录它应该去登录页面..如果他是,那么localhost: 3000应该把他带到产品目录....我真的很困惑...我甚至不知道如何做注销...

现在,正在登录并进入本地主机:3000重定向我到 http://localhost:3000/account

请帮忙。

我也注意到,如果我去到我的产品目录而被记录在我得到这个消息在上面F上的页面

“你必须注销访问此 页”

回答

2

纳乔,让我建议你看看http://railscasts.com/episodes/160-authlogic

它应该回答你所有的问题,以及更多。

关闭我的头顶......

开始,如果您还没有设置您的路线:

map.login 'login', :controller => 'user_sessions', :action => 'new' 

map.logout '退出',:控制器=> 'user_sessions' ,:动作=> '摧毁'

接下来,在你的应用程序控制器做这样的:

before_filter :authenticate, :except => [:login, :logout, :destroy, :index, :new] 

    private #-------------------- 

    def authenticate 
    unless current_user 
     flash[:notice] = "You must be loged in first" 
     redirect_to(login_url) 
     return false 
    end 
    end 

    def current_user_session 
    return @current_user_session if defined?(@current_user_session) 
    @current_user_session = UserSession.find 
    end 

    def current_user 
    return @current_user if defined?(@current_user) 
    @current_user = current_user_session && current_user_session.record 
    end 

这应该让你解决上述问题。如果人们没有登录,他们将被重定向到登录页面。此外,注销只是指向logout_url(本地主机:3000 /注销)