2010-06-15 57 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