Creating Resources

Recall our changes to the router.ex file, when we added the resources "/users" macro to router.ex to build a set of conventional routes. One new route maps posts to "/users" to the UserController.create action. Add a create function to UserController:

 def​ create(conn, %{​"​​user"​ => user_params}) ​do
  changeset = User.changeset(%User{}, user_params)
  {​:ok​, user} = Repo.insert(changeset)
  |> put_flash(​:info​, ​"​​#{​​}​​ created!"​)
  |> redirect(​to:​ user_path(conn, ​:index​))

This pattern of code should be getting familiar to you by now. We keep piping functions together until the conn has the final result that we want. Each ...

Get Programming Phoenix now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.