Routing to wrong function even if all looks good

Hello,

I get a wierd problem i never saw on my project. I store articles in my database and i have the basics functions to get all articles and get only one with it’s ID. However when I try this second function (show) it goes to my first function (index)

Here is the router (Nothing else than the resources defined) :

    #Resources
    resources "/users", UserController, except: [:new, :edit, :update, :create, :delete, :show]
    resources "/cryptos", CryptoController, except: [:new, :edit, :update]
    resources "/articles", ArticleController, except: [:new, :edit]
    resources "/subscribers", UserCryptoController, except: [:new, :edit, :update, :create, :delete, :index]

    get "/", PageController, :index
  end

Here are the controller functions :

  #Any user
  def index(conn, _params) do
    preloads = [:crypto]
    articles = Ressources.list_articles(preloads)
    render(conn, "index.json", articles: articles)
  end

  #Any user
  def show(conn, %{"id" => id}) do
    preloads = [:crypto]
    article = Ressources.get_article!(id, preloads)
    if article != nil do
      render(conn, "show.json", article: article)
    else
      conn
      |> put_view(CryptoApiWeb.ErrorView)
      |> render("bad_arguments.json")
    end
  end

Here is the phx.routes output for article :

       article_path  GET     /articles                              CryptoApiWeb.ArticleController :index
       article_path  GET     /articles/:id                          CryptoApiWeb.ArticleController :show
       article_path  POST    /articles                              CryptoApiWeb.ArticleController :create
       article_path  PATCH   /articles/:id                          CryptoApiWeb.ArticleController :update
                     PUT     /articles/:id                          CryptoApiWeb.ArticleController :update
       article_path  DELETE  /articles/:id                          CryptoApiWeb.ArticleController :delete

And lastly if needed an example of my call in url (Postman) :

Can’t figure out the problem…

Thanks for your time

Hi,

your URL should be /articles/4b1c… not /articles?id=4b1c… to trigger your show action.

1 Like

I can’t tell how dumb I am :') I tried because this was my through but the id was something else

Anyway thanks :')