In this tutorial, I would be teaching you how to setup up Elixir and Phoenix on your local machine and create a simple CRUD application in minutes.
Overview
Elixir is a dynamic, functional language designed for building scalable and maintainable applications. Elixir leverages the Erlang VM, known for running low-latency, distributed and fault-tolerant systems, while also being successfully used in web development and the embedded software domain.
Install Elixir
You can find all the information you need for installing Elixir on different operating systems here.
Install Phoenix
Run the following commands to install Phoenix. You can also get more details on this here.
$ mix local.hex $ mix archive.install hex phx_new 1.4.14
Database Setup
Phoenix is configured to use PostgreSQL relational database for its applications by default. If you do not have PostgreSQL on your machine you can install it from here.
Create Phoenix App
Let’s create a project named tasklist.
$ mix phx.new tasklist
You will be prompt to install dependencies(Y/n). If you want to create app without database, then you can select `--no-ecto` option.
To create your database, run the command below. You will also need to configure your database in config/dev.exs.
# Configure your database
config :tasklist, Tasklist.Repo,
username: "postgres",
password: "postgres",
database: "tasklist_dev",
hostname: "localhost",
show_sensitive_data_on_connection_error: true,
pool_size: 10
$ cd tasklist $ mix ecto.create
Once your database has been successfully created, start your Phoenix app with:
$ mix phx.server
Now, Open `localhost:4000` in browser, you will see screen like below
Build your CRUD
The mix phoenix.gen.html command helps us to quickly scaffold parts of our application by creating models, views, controllers, templates, migrations and test files.
$ mix phx.gen.html Functions Task tasks title:string description:string
Next, Add the resource to your browser scope in lib/tasklist_web/router.ex: :
resources "/tasks", TaskController
Remember to update your repository by running migrations, this command will create task table on database.
$ mix ecto.migrate
The line above defines all the the routes for our CRUD. It is an alternative to writing all the individual routes which can be viewed by typing:
$ mix phx.routes
page_path GET / TasklistWeb.PageController :index websocket WS /socket/websocket TasklistWeb.UserSocket
You can now restart your server and navigate to the browser tab your application is on, then change the URL to the ‘/tasks’ path. And you are done!
Categories: #Elixir Tags: #Elixir #Phoenix #Open Source #CRUD Application #PostgreSQL