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)
 
  conn
  |> put_flash(​:info​, ​"​​#{​user.name​}​​ created!"​)
  |> redirect(​to:​ user_path(conn, ​:index​))
 end

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 O’Reilly online learning.

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