Understanding Forms and Changesets: Difference between revisions
From ElixirBlocks
m (Admin moved page How to Understand Forms and Changesets to Understanding Forms and Changesets) |
No edit summary |
||
Line 6: | Line 6: | ||
To begin, create a new empty Phoenix app named app. | To begin, create a new empty Phoenix app named app. | ||
When complete, run this command to create database tables and Ecto context code. | |||
<source> | |||
mix phx.gen.context Items Item items name:string | |||
</source> | |||
==Seed Data== | |||
In the file named app/priv/repo/seeds.ex type the following code to create "dummy data" for this exercise. | |||
<source> | <source> | ||
App.Items.create_item(%{name: "item-1"}) | |||
App.Items.create_item(%{name: "item-2"}) | |||
App.Items.create_item(%{name: "item-3"}) | |||
</source> | |||
In the terminal type: | |||
mix | mix run priv/repo/seeds.exs | ||
==Routes== | |||
In routes, add the the following routes. | |||
<source> | |||
post "/items", ItemController, :create | |||
get "/items", ItemController, :index | |||
</source> | </source> | ||
In app/lib/app_web/controllers create a file controller named: | |||
item_controller.ex | |||
In the same directory create a |
Revision as of 11:45, 13 October 2023
This page is in progress
In this tutorial you create a database table named Item and its Ecto "Context" data. You learn how changesets work by writing controllers, template and forms.
To begin, create a new empty Phoenix app named app.
When complete, run this command to create database tables and Ecto context code.
mix phx.gen.context Items Item items name:string
Seed Data
In the file named app/priv/repo/seeds.ex type the following code to create "dummy data" for this exercise.
App.Items.create_item(%{name: "item-1"}) App.Items.create_item(%{name: "item-2"}) App.Items.create_item(%{name: "item-3"})
In the terminal type:
mix run priv/repo/seeds.exs
Routes
In routes, add the the following routes.
post "/items", ItemController, :create get "/items", ItemController, :index
In app/lib/app_web/controllers create a file controller named:
item_controller.ex
In the same directory create a