Challenge 3: render vs redirect_to — Solution class ProductsController < ApplicationController def update @product = Product.find(params[:id]) if @product.update(product_params) redirect_to @product else render :edit # If redirect_to :edit were used here instead of render :edit, the # browser would make a brand new GET request to the edit page -- # which would reload @product fresh from the database (discarding # the invalid changes the user just tried to submit) and lose the # validation error messages entirely, since those only exist on the # @product instance built during THIS failed update attempt. The # user would see a blank, unhelpful edit form with no indication of # what went wrong, instead of their original input plus error text. end end end =begin Notes: - redirect_to @product on success sends a fresh GET request to the product's show page, correctly reflecting the just-saved changes with a clean URL in the browser's address bar. - render :edit on failure re-renders the SAME @product object that just failed to save -- including both the user's submitted (invalid) values and any validation error messages attached to it -- without a new request being made at all. - The core rule from the chapter: render for "same request, show something related to what just happened"; redirect_to for "this succeeded, send the user somewhere new." =end