Setting up "New User" page - following the Phoenix book

Hello everybody

First of all, I hope it is allowed to ask questions about a specific book, yet since “Programming Phoenix” seems to be fairly widespread inside this circle, I’m bold enough to do so.

I tried to follow the guidelines and instructions in the book very closely, but now as I’m on page 65, I run into an - from my side - unexpected error which reads:
deps/ecto/lib/ecto/repo/queryable.ex:322: value "new" in where cannot be cast to type :id in query:

This is on http://localhost:4000/users/new

The error occurs after changing some of the code inside the user-controller.ex file. This is how my file looks like:

defmodule Rumbl.UserController do

  use Rumbl.Web, :controller

  def index(conn, _params) do
    users = Repo.all(Rumbl.User)
    render conn, "index.html", users: users
  end

  def show(conn, %{"id" => id}) do
    user = Repo.get(Rumbl.User, id)
    render conn, "show.html", user: user
  end

  alias Rumbl.User
  
  def new(conn, _params) do
    changeset = User.changeset(%User{})
    render conn, "new.html", changeset: changeset
  end

end

I’m not sure how informative this is to you.

Is there possibly something wrong with this code (I have checked it and it should be like instructed) or can you conclude the problem sitting at another place / another file?

Thank you.

It is because the get /users/new is interpreted like get /users/:id

The problem is in your router.ex file

It should be (well, it is just a guess)

get ​"​​/users/new"​, UserController, ​:new​
​get ​"​​/users/:id"​, UserController, ​:show​
2 Likes

Thanks, and indeed the problem was sitting in the router.ex file.

So, I changed the main scope of router.ex to this:

  scope "/", Rumbl do
    pipe_through :browser
    get "/", PageController, :index
    resources "/users", UserController, only: [:index, :show, :new, :create]
  end
1 Like