No route found for GET /en/students

Hello,
I generated a context and a schema using the following command

mix phx.gen.html Accounts Student  students username:string:unique password:string firstName:string lastName:string birthdate:date gender:string email:string:unique uniID:string 

then I added the routes to `router.ex’ but whenever I try to create a new student I get the following error

[debug] ** (Phoenix.Router.NoRouteError) no route found for GET /en/students (LeanWeb.Router)
    (lean) lib/phoenix/router.ex:324: LeanWeb.Router.call/2
    (lean) lib/lean_web/endpoint.ex:1: LeanWeb.Endpoint.plug_builder_call/2
    (lean) lib/plug/debugger.ex:122: LeanWeb.Endpoint."call (overridable 3)"/2
    (lean) lib/lean_web/endpoint.ex:1: LeanWeb.Endpoint.call/2
    (phoenix) lib/phoenix/endpoint/cowboy2_handler.ex:42: Phoenix.Endpoint.Cowboy2Handler.init/4
    (cowboy) d:/Work/leandeploy/deps/cowboy/src/cowboy_handler.erl:41: :cowboy_handler.execute/2
    (cowboy) d:/Work/leandeploy/deps/cowboy/src/cowboy_stream_h.erl:320: :cowboy_stream_h.execute/3
    (cowboy) d:/Work/leandeploy/deps/cowboy/src/cowboy_stream_h.erl:302: :cowboy_stream_h.request_process/3
    (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

here is my router file:

defmodule LeanWeb.Router do

  use LeanWeb, :router

  pipeline :browser do

    plug :accepts, ["html"]

    plug :fetch_session

    plug :fetch_flash

    plug :protect_from_forgery

    plug :put_secure_browser_headers

    plug SetLocale, gettext: LeanWeb.Gettext, default_locale: "en"

  end

  pipeline :api do

    plug :accepts, ["json"]

  end

  scope "/:locale", LeanWeb do

    pipe_through :browser

    get "/", PageController, :index

    resources "/students", StudentController, only: [:create, :new]

  end

  scope "/", LeanWeb do

    pipe_through :browser

    get "/", PageController, :index

    resources "/students", StudentController, only: [:create, :new]

  end

  # Other scopes may use custom stacks.

  # scope "/api", LeanWeb do

  #   pipe_through :api

  # end

end

What am I doing wrong?

Thanks in advance!

vs
only: [:create, :new]

I assume you have a :show or :index maybe?

I don’t have :index nor :show it automatically redirects me to this route when I submit a new student creation form.

@outlog was trying to say that your log here

[debug] ** (Phoenix.Router.NoRouteError) no route found for GET /en/students (LeanWeb.Router)

sends a request with GET method to path equals /en/students which triggers :index(routing defaults) action which is according to your router

resources “/students”, StudentController, only: [:create, :new]

is not defined. So the get the list you have to

  1. add :index to actions
  2. Ensure that this action is implemented in StudentController
def index(conn, _params
  # Code here to SturdentsContent.get_students_list() and render
end

If it is missing then seems you misgenerated the content and the controller.

1 Like

I think it’s because you have a route that was automatically generated for GET /students but you are passing a GET /en/students route, ie, using the en locale in the path before /students. You need to scope it correctly. Read more in here: Routing — Phoenix v1.7.10

1 Like

I believe that the error is caused by my router and especially that scoping with :locale I read the documentation but I could not figure out what is exactly wrong, could you please help?
Thanks!

When you use this command mix phx.routes what do you get?

That’s what I get:

     page_path  GET     /:locale                               LeanWeb.PageController :index
     student_path  GET     /:locale/students                      LeanWeb.StudentController :index
     student_path  GET     /:locale/students/:id/edit             LeanWeb.StudentController :edit
     student_path  GET     /:locale/students/new                  LeanWeb.StudentController :new
     student_path  GET     /:locale/students/:id                  LeanWeb.StudentController :show
     student_path  POST    /:locale/students                      LeanWeb.StudentController :create
     student_path  PATCH   /:locale/students/:id                  LeanWeb.StudentController :update
                   PUT     /:locale/students/:id                  LeanWeb.StudentController :update
     student_path  DELETE  /:locale/students/:id                  LeanWeb.StudentController :delete
     page_path  GET     /                                      LeanWeb.PageController :index
     student_path  GET     /students                              LeanWeb.StudentController :index
     student_path  GET     /students/:id/edit                     LeanWeb.StudentController :edit
     student_path  GET     /students/new                          LeanWeb.StudentController :new
     student_path  GET     /students/:id                          LeanWeb.StudentController :show
     student_path  POST    /students                              LeanWeb.StudentController :create
     student_path  PATCH   /students/:id                          LeanWeb.StudentController :update
                   PUT     /students/:id                          LeanWeb.StudentController :update
     student_path  DELETE  /students/:id                          LeanWeb.StudentController :delete
        websocket  WS      /socket/websocket                      LeanWeb.UserSocket

You can see that you have some routes scoped to an atom :locale not a variable that adapts to your locale on that request.
For example, if on your router you change scope "/:locale", LeanWeb do to scope "/en", LeanWeb do everything will work for a GET en/students.
So, either you find how to have there your locale as a variable or function (I don’t know how to do it) or you need to create different scopes for each locale (which is not efficient nor elegant) or don’t use the locale on your path to route requests (use another approach.

1 Like

Thank you so much, I worked it around by sending the :locale inside Router.Helpers.