Just some assorted notes I've made while digging into Phoenix, Elixir and friends. I'm coming from a strong Rails background, so many of these will refer to features from that framework.
Unlike Rails, where rendering is almost always performed by a template file, the responsibility of rendering a response in Phoenix lies with a view module (that typically corresponds to the current controller module.) This view module will typically offer a whole bunch of render
functions (matching different parameters, first and foremost the template name.) Templates (found in web/templates/
) will directly compile into such functions.
Yup: Slime and phoenix_slime. There's also phoenix_haml if you prefer that syntax.
Yup, Turbolinks 5 is easy enough to integrate into non-Rails apps. This blog post has details.
-
In a migration, add a unique index to your database:
create unique_index(:users, [:email])
-
Teach your Changeset to handle index violation errors reported by the database:
cast(user, params, ~w(email), ~w()) |> unique_constraint(:email)
Yup: priv/repo/seeds.exs
. It's a plain Elixir Script that you can run through mix run priv/repo/seeds.exs
.
Phoenix uses Brunch for assets, one of the many asset handlers from the domain of Node.js/Javascript. Core configuration is in your project's brunch-config.js
file.
Phoenix is configured to automatically recompile assets when they change. If you want to use another build tool than Brunch, apparently this is possible, but you need to set up automatic recompilation yourself.
By default, Brunch will concatenate CSS files found in web/static/css
in alphabetical order, but it will always use the default app.css
last. (You can change this in brunch-config.js
.)
Assuming you're using the default Brunch setup, just add the sass-brunch
npm package through NPM or Yarn. You'll probably want to rename your app.css
to app.scss
(to enable Sass syntax) and change the related entry in brunch-config.js
(to make sure the file is inserted last.)
defmodule MyApp.PageControllerTest do
use MyApp.ConnCase
test "GET /", %{conn: conn} do
conn = get conn, "/"
assert html_response(conn, 200) =~ "Welcome to Phoenix!"
end
end