登陆后转向用户访问的网页

在ASP.NET里面,通过设置强制登录可以让没有登录的用户首先跳转到登录页面,然后可以设置让用户登录后自动跳转到用户想要浏览的网页。同样,在Rails里面一样可以实现,实现过程如下:
A Friendlier Login System
As the code stands now, if an administrator tries to access a restricted
page before they are logged in, they are taken to the login page. When
they then log in, the standard status page is displayed—their original
request is forgotten. If you want, you can change the applicaton to forward
them to their originally-requested page once they log in.
First, in the authorize( ) method, remember the incoming request’s URI in
the session if you need to log the user in.

def authorize
unless User.find_by_id(session[:user_id])
session[:original_uri] = request.request_uri
flash[:notice] = "Please log in"
redirect_to(:controller => "login" , :action => "login" )
end
end

Once we log someone in, we can then check to see if there’s a URI stored
in the session and redirect to it if so.

def login
session[:user_id] = nil
if request.post?
user = User.login(params[:name], params[:password])
if user
session[:user_id] = user.id
redirect_to(session[:original_uri] || { :action => "index" })
else
flash[:notice] = "Invalid user/password combination"
end
end
end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值