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
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: https://hexdocs.pm/phoenix/routing.html#content
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!
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.