O'Reilly logo

Programming Phoenix by Jose Valim, Bruce Tate, Chris McCord

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required