Published January 16, 2017
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
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
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 :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
And great - our database was created successfully.
Now let’s start up our blog with
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.
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
And in it all the functions needed to create, edit, and delete posts have been populated for us.
It also created a corresponding in
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.
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.