我刚刚完成设置我的应用程序,以便在this完全
后使用authlogic在开始之前我已经创建了一个具有基本CRUD功能的产品目录。
现在我希望产品目录只有在用户登录时才可访问..所以,基本上如果用户没有登录,它应该进入登录页面..如果是,则localhost:3000应该把他带到产品目录....我真的很困惑......我甚至不知道该怎么办才能退出......
相反,现在,登录并转到localhost:3000会将我重定向到 http://localhost:3000/account
请帮忙。我也注意到,如果我在登录时访问我的产品目录,我会在页面顶部显示此消息
“您必须退出才能访问此内容 网页“
答案 0 :(得分:2)
Nacho,我建议你看http://railscasts.com/episodes/160-authlogic
它应该回答您的所有问题,等等。
脱离我的头顶......
如果您还没有设置路线,请先着手:
map.login 'login', :controller => 'user_sessions', :action => 'new'
map.logout'logout',: controller => 'user_sessions',:action => '销毁'
接下来,在您的应用程序控制器中执行此操作:
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)