Andrey
Andrey

Reputation: 321

Elixir generates a url with "?" instead of "/"

I have this route:

    resources "/my_items", ItemController, only: [:index, :show]
    get "/pages/:page_number", ItemController, :page, param: "page_number"

I have actions in ItemController for pages, it's a simplified version:

def index(conn, p) do
  # .....


def page(conn, %{"page_number" => page_num) when is_nil(page_num) do
  index(conn, %{"page_number" => 1})
end

def page(conn, %{"page_number" => page_num) do
  index(conn, %{"page_number" => page_num})
end

When I go to "/pages/123", it works fine. However, this:

items_path(conn, :page, page_number:  456)

throws an exception:

protocol Phoenix.Param not implemented for [page_number: 456]

Upvotes: 0

Views: 54

Answers (1)

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121000

You expect to get a map (basically, a Phoenix.Param to be converted to a map there):

#          ⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓⇓
page(conn, %{"page_number" => page_num ...

and what you are passing there is a Keyword, that is attempted to be converted to a Param. Since you already declared the param name, just do:

items_path(conn, :page, 456)

Sidenote: your page declarations are not valid Elixir code: closing curly brace is required there.

Upvotes: 2

Related Questions