我试图将用户重定向回他们在登录之前尝试访问的页面(该页面仅对用户可见)。
我将从针对我的旅行新操作的之前过滤器开始:
before_filter :authenticate, :only => [:new, :create, :edit, :update, :destroy]
def authenticate
deny_access unless signed_in?
end
def deny_access
store_location
redirect_to login_path, :notice => "Please log in to access this page."
end
def store_location
session[:return_to] = request.fullpath
end
这是我的sessions_controller.rb 中的新操作和创建操作
def new
@title = "Log in"
end
def create
user = User.authenticate(params[:session][:email].downcase,
params[:session][:password])
if user.nil?
flash.now[:error] = "Invalid email/password combination."
@title = "Log in"
render 'new'
else
sign_in user
redirect_back_or(root_path)
end
end
def redirect_back_or(default)
redirect_to(session[:return_to] || default)
clear_return_to
end
Thanks!
编辑:删除了 MaddHacker 的代码版本,因为这不是我想要的。
我通常使用 before_filter 之类的
def login_required
session[:return_to] = request.fullpath
redirect_to(login_page_url)
end
然后,当我的登录完成时,我在会话中查找 return_to:
def login
# user validation of login here
url = session[:return_to] || root_path
session[:return_to] = nil
url = root_path if url.eql?('/logout')
logger.debug "URL to redirect to: #{url}"
redirect_to(url)
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)