@product = Product.find(params[:id])
@product.attributes = params[:product]
image = Image.new params[:main_image]
@product.main_image.destroy if @product.main_image
@product.main_image = image
if @product.errors.empty? and @product.save flash[:notice] = "Product was successfully updated" redirect_to :action => "show", :id => @product else render :action => 'edit' end
Currently, products belong_to images, but later, I’ll probably have
products has_many images.
Say an image is invalid. So it won’t be saved. How do I properly
show that to the user? The above is my approach, where I have to
manually add image.errors.full_messages to the product.errors. But
then, I noticed that when an image had an error (and the error text
was added to product.errors), product still happily saved itself. So
that’s why there’s a check in there for product.errors.empty?.
I know there’a better way to do this that I’m missing…