What is the correct way to alter a ecto model in Phoenix?
So far I've created a Post
model which contains a title
and a content
string field. I added the resource to the router to create and view the Posts.
Now I would like to add a author
field to my model.
What I did until now is the following:
I created a migration with
mix ecto.gen.migration add_author_to_post
In the migrationfile I inserted
alter table(:posts) do
add :author, :string
end
and ran the migration with mix ecto.migrate
.
Now the coloumn is visible in the database.
Then I added the fileld to my model file:
schema "posts" do
field :title, :string
field :content, :string
field :author, :string
timestamps
end
@required_fields ~w(title content author)
@optional_fields ~w()
But when I visit the page to add a new Post, there is no field to add an author in the html.file.
(The new.html.eex
file was generated by the mix phoenix.gen.html
)
PS: I'm sure this is a basic beginner question. But since the phoenixcommunity is quite small, this question might be usefull for lots of other people
The template is not based on the current schema defined in your model. It is based on what you pass the mix phoenix.gen.html
task.
You will need to update your template to include the author in web/templates/post/form.html.eex
<div class="form-group">
<label>Author</label>
<%= text_input f, :author, class: "form-control" %>
</div>