纳乔,我建议你看http://railscasts.com/episodes/160-authlogic
它应该回答你所有的问题,等等。
从我的头顶上...
如果您还没有,请从设置路线开始:
map.login 'login', :controller => 'user_sessions', :action => 'new'
map.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)