Reputation: 415
If I delete a product from the database, the link associated with it no longer works and returns a 404 error such as
https://www.luminoto.com/wallpapers/burnt-down-forrest-ca-2006
In rails, how can I make sure that all deleted products get redirected to my homepage saying that product longer exists?
Upvotes: 0
Views: 59
Reputation: 5112
if its just a matter of showing link which actually do not work..you can use link_to_if
Creates a link tag of the given name using a URL created by the set of options if condition is true, otherwise only the name is returned.
<%= link_to_if(@product.present?, "Show More", { controller: "sessions", action: "new" }) %>
Upvotes: 0
Reputation: 5269
You can do something like:
class ProductsController < ActionController::Base
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
private def record_not_found
redirect_to controller: 'home', action: 'index', alert: 'Product no longer exists'
end
end
Check http://guides.rubyonrails.org/action_controller_overview.html#rescue-from for more info.
Upvotes: 4