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 Users

The create action in the UserController must now use our new registration_changeset, like this:

 def​ create(conn, %{​"​​user"​ => user_params}) ​do
  changeset = User.registration_changeset(%User{}, user_params)
 case​ Repo.insert(changeset) ​do
  {​:ok​, user} ->
  conn
  |> put_flash(​:info​, ​"​​#{​user.name​}​​ created!"​)
  |> redirect(​to:​ user_path(conn, ​:index​))
  {​:error​, changeset} ->
  render(conn, ​"​​new.html"​, ​changeset:​ changeset)
 end
 end

We use pattern matching to pick off the user_params from the inbound form. We create a registration changeset, and if it’s valid, we insert it and present the result to the user. If not, ...

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