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

Building Forms

Now that we have a database-backed repository, let’s add the ability to create new users in our system. We’re going to use Phoenix’s form builders for that purpose. First, open up your controller at web/controllers/user_controller.ex and set up a new user record for our new template, like this:

 alias Rumbl.User
 
 def​ new(conn, _params) ​do
  changeset = User.changeset(%User{})
  render conn, ​"​​new.html"​, ​changeset:​ changeset
 end

Notice the User.changeset function. This function receives a struct and the controller parameter, and returns an Ecto.Changeset. Changesets let Ecto manage record changes, cast parameters, and perform validations. We use a changeset ...

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