Phoenix error updating
The insert function returns a tuple that indicates success or failure.
We use pattern matching to determine what HTTP status and data to render into JSON.
We now have a very simple REST API to create, read and update this simple user object.
I’ve purposely not performed any validation (other than uniqueness) on the model as I intend to use this in conjunction with an Elm front-end that performs validation.
Eventually I will add more validation and return that validation back to the Elm front end.
I found that creating the create (POST) and update (PUT) endpoints was relatively straightforward.
In the fail case, an Elixir map is returned with a list of error strings.
If you now use a REST client to perform put operations, you will see that valid data is properly written.
The only major difference is the separation of the changeset from the model.You can use the get endpoint to conform the proper writing of data.Our final major task will be to allow the update of existing records using a put endpoint. The put request received an additional parameter which contains the id of the record being updated.With this function in place, I then modify the two controller actions to add this transformation.Given that currently this function will only be used within this single controller, I made it a private function using the This allows the POST request to run, but nothing is written to the database yet.
Phoenix provides a function that adds the proper status code and returns a new conn with that status.