Ayman Arif
Ayman Arif

Reputation: 1689

Undefined function static_path/2 in <script> tag

I am using Embedded Elixir for templated HTML.

<script src="<%= static_path(@conn, '../js/app.js') %>"> </script>

This line is giving me the following error:

== Compilation error in file lib/chat_web/views/layout_view.ex ==
** (CompileError) lib/chat_web/templates/layout/app.html.eex:24: undefined function static_path/2
(elixir 1.10.2) src/elixir_locals.erl:114: anonymous fn/3 in : 
:elixir_locals.ensure_no_undefined_local/3
(stdlib 3.12.1) erl_eval.erl:680: :erl_eval.do_apply/6
(elixir 1.10.2) lib/kernel/parallel_compiler.ex:304: anonymous fn/4 in 
Kernel.ParallelCompiler.spawn_workers/7

Upvotes: 2

Views: 1606

Answers (1)

sbacarob
sbacarob

Reputation: 2212

The static_path function comes from YourAppWeb.Router.Helpers (before Phoenix 1.4, it came from YourApp.Router.Helpers), but the important thing is that before 1.4, views would import YourApp.Router.Helpers, thus making it available in your views and templates, but from 1.4 onwards, views alias YourAppWeb.Router.Helpers, as: Routes (you can verify this in your apps' web.ex file), therefore you can access the helper functions using Routes.<function>.

so, as suggested in my comment, your example should work with:

<script src="<%= Routes.static_path(@conn, '../js/app.js') %>"> </script>

Upvotes: 4

Related Questions