2010-06-15 54 views
3

我正在寻找最佳实践解决方案,以便能够在成功销毁操作后继续使用重定向:返回,因为许多项目可以从各种列表中删除。抢救重定向:在Rails中销毁之后回来?

不幸的是,当从项目视图本身启动删除时,策略失败。

您对这种情况推荐什么方法?

回答

5

你需要考虑,如果一个项目是从自己的看法网页上删除了你想要什么样的行为..

我建议两个选项之一:

保持你的redirect :back,并实施一些如果请求的资源已不存在那种第二重定向 - 即/products/10重定向到/products

@product = Product.find_by_id(params[:id]) # although I admit I'm not sure 
redirect_to products_path unless @product # about this 

或更改重定向如果特定路径匹配摧毁一个

@product.destroy # you might need to save the path before you destroy the object.. 
redirect_to :back and return unless request.referrer == product_path(@product) 
redirect_to products_path 

我不认为这种情况有一套一套的标准,但我可能会纠正。

+0

我喜欢你的第二个选项,因为它避免了另一个重定向。谢谢。 – Andreas 2010-06-15 19:44:15

+0

纠错:redirect_to:返回并返回除非request.referer == product_path(@product) – Andreas 2010-06-15 22:35:54

+0

修正,谢谢:) – Jeriko 2010-06-15 22:42:45