Lesson 19. Creating and reading your models

 

In lesson 18, you constructed your user model and built an index page to display users on the same page. In this lesson, you add more functionality to your application by focusing on the create and read functions of CRUD. You start by creating an EJS form that handles a user’s attributes as inputs. Then you create the routes and actions to handle that form data. Last, you build a show page to act as the user’s profile page.

This lesson covers

  • Constructing a model creation form
  • Saving users to your database from the browser
  • Displaying associated models in a view
Consider this

With a new way to create courses for your recipe application, you’re finding it tedious to add individual documents to your database on REPL. You decided to create dedicated routes to create new model instances, edit them, and display their data. These routes are the foundations of CRUD methods that allow interaction with your data to flow through your application views.

19.1. Building the new user form

To create a new user instance in your database, you need some way of retrieving that user’s data. So far, you’ve been entering that data directly in REPL. Because you’re moving all your data interactions to the browser, you need a form through which new users can create their accounts. In CRUD terms, that form lives in a view called new.ejs.

19.2. Creating new users from a view

19.3. Reading user data with show

Summary