【问题标题】:help with authlogic帮助 authlogic
【发布时间】:2009-11-09 22:16:41
【问题描述】:

我刚刚完成设置我的应用程序以使用 authlogic,完全遵循 this

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

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

相反,现在,登录并转到 localhost:3000 会将我重定向到 http://localhost:3000/account

请帮忙。

我还注意到,如果我在登录时访问我的产品目录,我会在页面顶部看到此消息

"您必须先登出才能访问此 页”

【问题讨论】:

    标签: ruby-on-rails authlogic


    【解决方案1】:

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

    它应该回答你所有的问题,等等。

    在我的头顶...

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

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

    map.logout 'logout', :controller => 'user_sessions', :action => 'destroy'

    接下来,在您的应用程序控制器中执行此操作:

      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 (localhost:3000/logout)

    【讨论】:

      猜你喜欢
      • 2010-12-27
      • 2018-12-21
      • 2011-06-20
      • 2011-07-28
      • 2011-10-04
      • 2011-08-03
      • 2011-11-27
      • 1970-01-01
      • 2011-08-16
      相关资源
      最近更新 更多