5

I have this in my router.ex:

resources "/games", GamesController do
  get "/scores/:student_id", GameScoreController, :new
  post "/scores/:student_id", GameScoreController, :create
end

Now I am calling this with:

link(student.name, to: game_game_score_path(@conn, :new, @game, student_id: student))

But that creates a link: /games/1/scores?student_id=1 instead of /games/1/scores/1.

How do I call link so it generates the correct url?

Oh, and is there a way to get rid of the double game in the helper? I tried adding as: :game_score, but that did not change anything.

Paweł Obrok
  • 22,568
  • 8
  • 74
  • 70

1 Answers1

2

Define the route as:

resources "/games", GameController do
  get "/scores/:student_id", ScoreController, :new
  post "/scores/:student_id", ScoreController, :create
end

Note everything in Phoenix is singular, including the controller name. The URL is plural but it is external, it doesn't dictate how your code is organized.

And the URL helper:

link(student.name, to: game_game_score_path(@conn, :new, @game, student))

If you want the controller to be GameScoreController, you can pass the :as option to customize the generated helper. More info in the Phoenix docs: http://hexdocs.pm/phoenix/Phoenix.Router.html#resources/4

José Valim
  • 50,409
  • 12
  • 130
  • 115
  • 1
    I don't know what I changed, except adding a 3rd route, but the link suddenly works. My controller is GameScoreController, I needed to use as: :score, and now that works too. Perfect! Thanks! – Herman verschooten Sep 13 '15 at 10:10