Use link to: in a Phoenix template

I’m reading Phoenix for Rails developers book and the following line raises a compilation exception:

<%= link to: book_path(@conn, :show, do %>
<% end %>

The compilation error stack trace:

== Compilation error in file lib/storex_web/views/book_view.ex ==
** (CompileError) lib/storex_web/templates/book/index.html.eex:17: undefined function book_path/3
    (elixir) src/elixir_locals.erl:108: :elixir_locals."-ensure_no_undefined_local/3-lc$^0/1-0-"/2
    (elixir) src/elixir_locals.erl:109: anonymous fn/3 in :elixir_locals.ensure_no_undefined_local/3
    (stdlib) erl_eval.erl:680: :erl_eval.do_apply/6
    (elixir) lib/kernel/parallel_compiler.ex:229: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7

What’s wrong with that syntax? I do have the needed route defined:


scope "/", StorexWeb do
    pipe_through :browser

    get "/", BookController, :index
    get "/books/:id", BookController, :show

and the routes are well defined:

book_path  GET  /                                      StorexWeb.BookController :index
book_path  GET  /books/:id                             StorexWeb.BookController :show
websocket  WS   /socket/websocket                      StorexWeb.UserSocket

Thank you.

Can you please show us content of:

  1. lib/storex_web.ex (which should contain module StorexWeb)
  2. lib/storex_web/views/book_view.ex (which should contain module StorexWeb.Views.BookView)

There is probably something missing in one of them …

It’s probably because You are following a book from a previous Phoenix version. With the latest Phoenix, You need to prepend Routes to your path… like this

<%= link to: Routes.book_path(@conn, :show, do %>
1 Like

You are right I just noticed it too and wanted to edit my response, but you were first :slight_smile:

1 Like

Ah, cool, thanks a lot, guys. That was that :slight_smile: