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

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.