当前位置:  开发笔记 > 编程语言 > 正文

Ruby on Rails-存储当前网址以在销毁后重定向

如何解决《RubyonRails-存储当前网址以在销毁后重定向》经验,为你挑选了1个好方法。



1> SteveTurczyn..:

您可以在ApplicationController中存储多个redirect_to路径...最好使用单独的before_action来执行此操作

before_action :store_back_paths

def store_back_paths

  # if session[:back_path] doesn't exist, create it as an empty array

  session[:back_path] ||= []

  # add the current path as a new entry in the array

  session[:back_path] << request.referer

  # while there are more than five entries, drop the oldest entries 
  # this is to ensure we're not storing too many entries

  session[:back_path].shift while session[:back_path].count > 5
end

进行普通重定向时,请使用pop删除数组中的最后一个元素并返回删除的值。

redirect_to session[:back_path].pop

在销毁后进行重定向时,请删除最后一个条目(指向销毁项目的显示页面),然后重定向到该条目之前的路径。

session[:back_path].pop # drops the last entry   
redirect_to session[:back_path].pop

推荐阅读
有风吹过best
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有