Subscribe to access all episodes. View plans →
Published January 16, 2017
Using:
Episode source code on GitHub
Getting started with a new language or framework can be intimidating so I wanted to show how easy - and fast - you can bootstrap an application using phoenix.
Our project will be a basic blog that will allow us to create, edit and delete posts. So let’s get started.
First we’ll generate a new phoenix project named ‘teacher’ from the command line.
$ mix phoenix.new teacher
And we’ll want to download the dependencies
$ Y
With the dependencies installed, we have a list of next steps.
Let’s change into the directory of our newly created blog.
$ cd teacher
And open dev.exs config/dev.exs
Phoenix ships with PostgreSQL as the default database.
Here you’ll need to update the username and password values to match what you use in your development environment.
config/dev.exs
config :teacher, Teacher.Repo,
username: “postgres”,
password: “postgres”,
database: “teacher_dev”,
We can also change the name of the database that our blog will use.
Now with our credentials all set, let’s go back to the terminal and create our database. We’ll use the ecto task mix ecto.create
And great - our database was created successfully.
Now let’s start up our blog with mix phoenix.server
And if we open our browser to localhost:4000, we should see our app running with the ‘Welcome to Phoenix’ template.
Now that we have our blog running our next step is to add posts to it.
In situations where you want to generate a full HTML resource, phoenix provides a task to set everything up for you.
Back in our terminal we’ll run this task:
$ mix phoenix.gen.html Post posts title:string body:text
Post is what we want to name our module.
Posts - the plural name - is what will be used to create the corresponding table in our database.
And then a list of attributes for our model - in this case we want a post to have a title that’s a string and a body that’s a text.
Put another way - this will add a title column to our posts table with a string data type and a body column with a text data type.
We can see from the message printed that we need to update our router with our post routes - so let’s do that.
web/router.ex
scope “/“, Teacher do
pipe_through :browser
get “/“, PageController, :index
resources "/posts", PostController
end
Now let’s take a look at what was created to get a better sense of what happened.
Let’s start with the controller web/controllers/posts_controller.ex
And in it all the functions needed to create, edit, and delete posts have been populated for us.
It also created a corresponding in web/views/post_view.ex
The primary responsibility of views in phoenix are to render templates.
And as for our templates, we can see they have been created for our different actions.
It also created a database migration for us.
priv/repo/migrations/{{timestamp}}_create_posts.exs
This migration creates a table named “posts” and adds our two columns to it: “title” and “body”.
Everything looks good, so we can migrate our database with another ecto task:
$ mix ecto.migrate
Great the “posts” table in our database was created.
Now another handy task is mix phoenix.routes
, which will print all the routes defined by our Phoenix app.
Running it we see we have a ‘/posts’ endpoint.
Let’s check it out.
Right now, we don’t have posts - so let’s create one.
We’ll click ‘New Post and give our posts a title and some content. And then click submit.
And great - our post was saved!
What’s great is that all of this was done by Phoenix. We were able to bootstrap a simple blogging application in hardly any time.
Stay tuned for our next episode where we’ll add comments to our blog posts.
David B
7 years agoHI, I’m using the latest version of Phoenix and Elixir, rain “mix phx.server” and I tried to load the /posts route, but I’m getting “ ** (UndefinedFunctionError) function BlogWeb.PostController.init/1 is undefined (module BlogWeb.PostController is not available)
… Sorry I’m a total newbie at this. Do you know what I’m doing wrong? Thanks in advance.
Alekx
7 years agoThis could be from the version of Phoenix. This episode uses Phoenix 1.2 and the latest version, 1.3, has a few changes to the directory structure, module names, etc.
imtinge
7 years agowill be updated to phoenix 1.3?
Alekx
7 years agoI hope to have some new episodes on using Phoenix 1.3 coming out. I’ll also look into revising this, but that may be a bit further out.
Thad Woodman
6 years agoI think the mix syntax may be deprecated. Currently getting a few error messages here for
mix phx.gen.html Post posts title:string body:text
, namely:** (Mix) Expected the schema, "posts", to be a valid module name
Thad Woodman
6 years agoAh, I think it expects a context now so something like this instead:
phx.gen.html Post Post posts title:string body:text
not sure about using the Post for the Context and Module name is advised but something like that should work
mn113
5 years agoIs there a way to see the list of videos on this site? I found #1 by chance. How on earth does anybody navigate here?
Alekx
5 years agoCurrently there’s the search box in the header that does support searching by episode number. You can also scroll to previous episodes using pagination.
I don’t have a single page that lists the episodes yet, but I’ll try and add that to help make navigation easier.
Alekx
5 years agoI’ve added a list of all episodes here: https://elixircasts.io/episodes
Hope this helps!