在restful_authentication插件应用中,有时要实现这样的功能:用户可以在不登录的情况下浏览一些页面;但当用户想进入需要权限的页面时,会自动跳转到登录界面;用户登录完成后,则自动跳转回用户登录前想要去的页面。
在restful_authentication插件中,要实现这样的功能,可以在session_controller.rb中修改create方法
还有一个地方要修改的就是在authenticated_system.rb中login_required方法和access_denied方法。
在restful_authentication插件中,要实现这样的功能,可以在session_controller.rb中修改create方法
def create
#认证相关代码
self.current_user = User.authenticate(params[:login], params[:password])
if logged_in?
...
#下面是要修改的跳转方法,登录成功时
redirect_to(
session[:return_to]
)
flash[:notice] = "登录成功"
else
#登录失败时
render :action => 'new'
flash[:notice] = "用户名或密码错误"
end
end
还有一个地方要修改的就是在authenticated_system.rb中login_required方法和access_denied方法。
def login_required
#下面这句"store_location"是要添加的
store_location
authorized? || access_denied
end
def access_denied
respond_to do |format|
format.html do
#将下面的"store_location"注释掉,否则会跳回登录页面
# store_location
redirect_to new_session_path
end
format.any do
request_http_basic_authentication 'Web Password'
end
end
end