So we worked on the product model to make the save method more flexible.
And now in the Edmond J as controller in post at a product there we already have code where we find
a product by id and this will work because I find by ID is a method I created in my model.
So here we have the product or maybe we should name it product data because here I will now create a
new product constant by using my product constructor's and my own product class and there I will pass
my updated title my updated price my updated image your L and my updated description and my ID.
No actually we don't even need find by that anymore.
We can just get rid of that and for now all of that and simply create a new product with all the updated
information and we now also need to pass the product ID there you just need to make sure that you create
a new one would be object id object.
So make sure that at the top of the file you do actually require mongered be and now you can of course
also create a new constant named object ID or whatever you want and extract that object id constructor
out off Mongo be like this.
And then you could just write new object id and reference this basically so we can now go down to post
edit product and pass a new object id here with new object id and wrap that Prod. ID you're extracting
from the you are l here and wrap that or from the body of the request and wrap that into object ID.
Now we have that new product and now we can simply call product safe because we just modified the safe
method to support both creation and updating.
So let me save all of that.
And let's give this a try.
Let's reload that view here maybe change the price to 999 and click Update product.
Now we get a page not found.
Let's ignore that for now and let's instead go back to Molly Compaq's and click that refresh.
I can hear you now that doesn't look like it worked though right.
The price is unchanged but I also did not get an error.
So what's wrong here.
Well you need to go to your app and chase routes again and uncomment does post edit product drought
otherwise editing products will not be possible.
Now if we load that product to edit again and we change that price now it looks much better or at least
partly if we have a look at more would be Compaq's we see now it looks like I made a mistake with the
assignment of values from my description and I can simply add at this your my description.
Now Assembly last you are l and the other way round.
So let me change both and click update here.
Now if we look at this page again it should work.
But the most important thing is our method worked.
Now what did I mess up.
If I go to the admin controller I'm assigning price image you your Elen description well that is in
the wrong order.
It should first be the description and then the image you are l but that was only a little logical mistake
I made.
If I edit this again back to twelfth 99 you see now this is working and this also works.
Let's see how it works if I added two fields at the same time.
Does all that works.
So this is how we can update a product with the help of Muang with a B and with the help of update 1.