我正在使用Devise来处理用户身份验证。我在另一个控制器视图中添加了一个符号。当用户通过此表单登录时,我想将它们重定向回该页面。我尝试用以下方法完成此任务:
应用程序控制器
override func tableView(tableView: UITableView, willSelectRowAtIndexPath indexPath: NSIndexPath) -> NSIndexPath? {
if let selectedIndexPaths = tableView.indexPathsForSelectedRows() {
for selectedIndexPath in selectedIndexPaths {
if indexPath.section == selectedIndexPath.section {
return nil
}
}
}
return indexPath
}
此代码的问题是当用户从外部源访问链接或键入页面的完整地址时,它不起作用。每当用户使用此特定表单登录时,我都需要一个有效的解决方案。
答案 0 :(得分:2)
def after_sign_in_path_for(resource)
sign_in_url = new_user_session_url
if request.referer == sign_in_url
super
else
stored_location_for(resource) || request.referer || root_path
end
end
在application_controller中添加以上内容。如果最后一个请求将被登录,那么它将调用父请求。
答案 1 :(得分:0)
如果您有before_filter:authenticate_user!在应用程序控制器中,您应该使用之前的过滤器存储您的位置。还要确保在authenticate_user之前放置!
before_filter :store_location