Once upon a time the quintessential learning project was hello world.  Now that’s not enough and its “Build a Blog”.  Well it was good enough to launch Ruby on Rails to its hallowed position so its likely good enough for Elixir and Phoenix.  I found the CodeTunes tutorial by google for “elixir blog” when I was actually looking for people blogging about Elixir but let’s get started.  

Issue 1: The Babelrc Debacle or es2015 alas...

So I ran thru the initial setup and generated the project skeleton.  When I looked at in a browser I noticed that the entire asset pipeline was missing and I was getting errors in the console like this:

05 Mar 09:29:34 - error: Compiling of web/static/js/app.js failed. Couldn't find preset "es2015" relative to directory "/Users/sjohnson/elixir/phoenix" ; Compiling of web/static/js/socket.js failed. Couldn't find preset "es2015" relative to directory "/Users/sjohnson/elixir/phoenix"

I’ve seen this before when I blew about 3 hours on my first attempt to get anything working with Elixir which ultimately resulted in my creating and closing a github issue on this topic.

Here’s the solution:

  1. touch .babelrc
  2. vi .babelrc
  3. Add this line:
  4. { "presets": ["es2015"] }

 Overall this looks a lot like something that, at least for now, you have to do every time you create an Elixir application at least if you want the asset pipeline to work.  There’s a long and furious rant related to the use of node / brunch over in /r/elixir and while the original poster’s message was lost in his rage, the basic point that here with something new and yet cargo culting over big parts of technology.  Yes node works and yes brunch works and, no, no one wants to rewrite the asset pipeline crap from scratch but this is at least part of the result.  Something to think about.

Other Issues

In working thru this tutorial I made a number of syntactic mistakes that made me recall the days of learning Rails.  Normal stuff including the "Fix protocol Ecto.Queryable not implemented error” which was a bit mysterious but Stack Overflow cleared up.  The issue was that I had started with this:

def index(conn, _params) do
posts = Repo.all(Post)
end

to which i added a pipeline like this:

def index(conn, _params) do
posts = Repo.all(Post)
|> Post.count_comments
|> Repo.all
render(conn, "index.html", posts: posts)
end

but that really should have been this:

def index(conn, _params) do
posts = Post
|> Post.count_comments
|> Repo.all
render(conn, "index.html", posts: posts)
end

Phoenix and Battery Life

One other thing I noticed is that the constant live reloading really pulls down my battery life on my Macbook Air.  Where I normally can work for several hours, I found myself down to 20% battery after only two hours.