How to not hardcode Path in Phoenix button?


I am creating a basic temple that has a button. This button is supposed to make a POST call to an endoint called "hangman/", like this:

<%= button("New Game", to: "/hangman") %>


In the course I am seeing, they mention this is a bad practice because I am hardcoding the path, so instead the author suggests a better approach - to use the hangman_path function:

mix phx.routes
hangman_path  GET   /hangman                               GallowsWeb.HangmanController :new_game
hangman_path  POST  /hangman                               GallowsWeb.HangmanController :create_game

Which would result in the following:

<%= button("New Game", to: hangman_path(@conn, :create_game)) %>

However this change doesn’t even compile. I am guessing this worked in the author’s code because he was using Phoenix 1.3 and it fails in mine because I am using the latest version of Phoenix (1.4.9)


What am I doing wrong ?

1 Like

As far as I remember, you need to qualify the route helpers in the recent Phoenix as Routes.foo_path or something.