Note: There is a new version of the book for Phoenix 1.4

Programming Phoenix 1.4

The following notes are for the original version of the book

​

https://tamalpais.atlassian.net/wiki/spaces/LosGatosRG/pages/243826697


Installation on mac

brew install erlang

erl --version
[Erlang/OPT 22… Eshell V10.6.2 (abort with ^G)]

brew install elixir

elixir -v
[Erlang/OTP 22 … Elixir 1.9.4 (compiled with Erlang/OPT 22]

mix local.hex

(node already installed)

node --version
[v10.13.0]

brew install postgresql

psql --version
[psql (PostreSQL) 12.1]

mix archive.install hex phx_new 1.4.12

mix phx.new --version
[Phoenix v1.4.12]

Installation of postgresql on ubuntu


sudo apt install -y postgresql

psql --version

sudo systemctl restart postgresql.service

sudo -u postgres psql template1

template1=# ALTER USER postgres with encrypted password ‘phoenix’;

template1=# quit (or something like that)

mix phx.new hello

cd hello/config

nano dev.exs

Change password to ‘phoenix’

cd ../assets

npm install - no need to do this anymore, it seems

cd ..

mix ecto.create

mix phx.server

Setting up Phoenix Hello World on mac


Notice that instead of ‘phoenix’, one needs now to use the word ‘phx’

1mix phx.new hello 2cd hello
1nano config/dev.exs

On line 5, change username to match mac user

(I also changed the hostname on line 8 to 127.0.0.1 from localhost, but this change probably didn’t matter)

1 initdb /usr/local/var/postgres2 2 pg_ctl -D /usr/local/var/postgres2 -l logfile start 3 mix ecto.create

Differences in code


1nano lib/hello_web/router.ex


1defmodule HelloWeb.Router do 2... 3 scope "/", HelloWeb do 4 pipe_through :browser 5 6 get "/hello/:name", HelloController, :world 7 get "/", PageController, :index 8 end 9end


1nano lib/hello_web/controlles/hello_controller.ex


1defmodule HelloWeb.HelloController do 2 use HelloWeb,:controller 3 4 def world(conn, %{"name" => name}) do 5 render conn, "world.html", name: name 6 end 7end


1nano lib/views/hello_view.ex


1defmodule HelloWeb.HelloView do 2 use HelloWeb, :view 3end


1nano lib/templates/hello/world.html.eex


1<h1>Hello <%= String.capitalize @name %>!</h1>