Phoenix: mix phx.gen.html creates wrong subfolder and wrong module name

Created on 25 Jul 2017  路  10Comments  路  Source: phoenixframework/phoenix

Environment

  • Elixir version (elixir -v): 1.4.5
  • Phoenix version (mix deps): 1.3.0-rc.3
  • NodeJS version (node -v): 8.2.1
  • NPM version (npm -v): 5.3.0
  • Operating system: Ubuntu 16.04
mix phx.new xx
mix ecto.create
mix phx.gen.html Web Movie movies name:string

Expected behavior

Expecting to see lib/xx/web folder. Expecting to find controller Xx.Web.MoviesController in lib/xx/web/controllers.
Expecting to see defmodule Xx.Web.MovieController do in lib/xx/web/controllers/movie_controller.ex

Actual behavior

Seeing both lib/xx and lib/xx_web folder. Being incorrectly instructed to:

Add the resource to your browser scope in lib/xx_web/router.ex:

    resources "/movies", MovieController

Folder xx_web doesn't contain router.ex, it's in xx folder.
Seeing defmodule XxWeb.MovieController do in lib/xx_web/controllers/movie_controller.ex

Most helpful comment

@roberthopman Because phx generates a Phoenix 1.3 app and phoenix are the generators for Phoenix 1.2 and below. The default project structure changed in Phoenix 1.3 and therefore the phoenix generators are not compatible.

All 10 comments

The Xx.Web namespace was changed to XxWeb in rc3, see https://elixirforum.com/t/phoenix-1-3-0-rc-3-released/7150/13

I see, thanks. I had to rerun mix archive.install https://github.com/phoenixframework/archives/raw/master/phx_new.ez
Seems I had an old version that was slightly broken.

Phoenix newbie here. I'm confused, should it work as expected now? I'm following along the guide at https://hexdocs.pm/phoenix/testing.html#content with Phoenix 1.3.0 and I'm bumping into issues.

$ mix phx.new hello
$ cd hello
$ mix ecto.create
$ mix phoenix.gen.html User users name:string email:string bio:string number_of_pets:integer

Help message says to edit web/router.ex but I don't have such a file, it appears to be lib/hello_web/router.ex

$ vi lib/hello_web/router.ex
$ mix text
** (CompileError) test/controllers/user_controller_test.exs:2: module Hello.ConnCase is not loaded and could not be found                                               
    (elixir) expanding macro: Kernel.use/1                                          
    test/controllers/user_controller_test.exs:2: Hello.UserControllerTest (module)  
    (elixir) lib/code.ex:376: Code.require_file/2                                   
    (elixir) lib/kernel/parallel_require.ex:59: anonymous fn/2 in Kernel.ParallelRequire.spawn_requires/5 

Apparently that's because ConnCase is under HelloWeb, not under Hello. I tried to fix that in test/hello_web/controllers/page_controller_test.exs but then got this error

$ mix test
...** (CompileError) test/models/user_test.exs:2: module Hello.ModelCase is not loaded and could not be found
    (elixir) expanding macro: Kernel.use/1
    test/models/user_test.exs:2: Hello.UserTest (module)
    (elixir) lib/code.ex:376: Code.require_file/2
    (elixir) lib/kernel/parallel_require.ex:59: anonymous fn/2 in Kernel.ParallelRequire.spawn_requires/5

I found a Hello.DataCase module that looks like it might be what it's looking for so I edited test/models/user_test.exs to replace ModelCase with DataCase and now I get this error:
` $ mix test ...** (CompileError) test/controllers/user_controller_test.exs:30: Hello.User.__struct__/1 is undefined, cannot expand struct Hello.User test/controllers/user_controller_test.exs:29: (module) (stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
And I got stuck on this one. Am I doing something wrong are there indeed issues with generated code?

@amarandon The following cannot work

$ mix phx.new hello
...
$ mix phoenix.gen.html

@webdeb Of course, good catch! All tests passing now :)

I got this from section "Generating More Files" of https://hexdocs.pm/phoenix/testing.html#content
I guess I should send a PR to update this guide, shouldn't I?

@amarandon Sure, why not. this is the repo https://github.com/phoenixframework/phoenix_guides

@webdeb OK, I noticed more problems in the guide so I added issue https://github.com/phoenixframework/phoenix_guides/issues/721 and intend to work on it. Thanks for your help!

@webdeb why does this not work?
$ mix phx.new hello
...
$ mix phoenix.gen.html

$ mix phoenix.gen.html

$ mix phx.gen.html

@roberthopman Because phx generates a Phoenix 1.3 app and phoenix are the generators for Phoenix 1.2 and below. The default project structure changed in Phoenix 1.3 and therefore the phoenix generators are not compatible.

Was this page helpful?
0 / 5 - 0 ratings