Redirection Rescue: Back After Destruction in Rails?

I am looking for a solution for best practice to be able to use redirection: back after the successful destruction action, since many elements can be removed from different lists.

Unfortunately, this strategy fails for one case where the deletion is initiated from the element view itself.

What approach do you recommend for this situation?

+3
source share
1 answer

You need to think about what behavior you want if the item is removed from its own view page.

I would suggest one of two options:

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

, , .

+5

Source: https://habr.com/ru/post/1750156/


All Articles