A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/phoenixframework/phoenix/issues/3832 below:

Unable to mix phx.gen.html --no-context (without Context) · Issue #3832 · phoenixframework/phoenix · GitHub

Hi! 👋

Trying to run the mix phx.gen.html with the --no-context and --no-schema flags because I only want to create a controller, view, templates and tests for a new resource.
As per the docs: https://hexdocs.pm/phoenix/Mix.Tasks.Phx.Gen.Html.html#module-customising-the-context-schema-tables-and-migrations

Note totally agree that there is definitely a use-case for Contexts in larger projects, but in a small project like the tutorial/example I'm building it just adds complexity without any benefit.

The command I've have attempted to run is:

mix phx.gen.html Quotes quotes author:string text:string --no-context --no-schema

But see the following error:

** (Mix) Expected the schema, "quotes", to be a valid module name

mix phx.gen.html, phx.gen.json, phx.gen.live, and phx.gen.context
expect a context module name, followed by singular and plural names
of the generated resource, ending with any number of attributes.
For example:

    mix phx.gen.html Accounts User users name:string
    mix phx.gen.json Accounts User users name:string
    mix phx.gen.live Accounts User users name:string
    mix phx.gen.context Accounts User users name:string

The context serves as the API boundary for the given resource.
Multiple resources may belong to a context and a resource may be
split over distinct contexts (such as Accounts.User and Payments.User).

The error message and examples suggest we must include a Context in as the first argument to mix phx.gen.html regardless of using the --no-context flag.

So added a dummy context to the mix phx.gen.html command:

mix phx.gen.html Ctx Quotes quotes author:string text:string --no-context --no-schema

And that appears to work in that we see the following output:

* creating lib/app_web/controllers/quotes_controller.ex
* creating lib/app_web/templates/quotes/edit.html.eex
* creating lib/app_web/templates/quotes/form.html.eex
* creating lib/app_web/templates/quotes/index.html.eex
* creating lib/app_web/templates/quotes/new.html.eex
* creating lib/app_web/templates/quotes/show.html.eex
* creating lib/app_web/views/quotes_view.ex
* creating test/app_web/controllers/quotes_controller_test.exs

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

    resources "/quotes", QuotesController

e.g: dwyl/phoenix-content-negotiation-tutorial@9a37b21

But when attempting to run the tests immediately after running the mix phx.gen.html command,
we see a compilation error:

== Compilation error in file lib/app_web/controllers/quotes_controller.ex ==
** (CompileError) lib/app_web/controllers/quotes_controller.ex:13: App.Ctx.Quotes.__struct__/1 is undefined, cannot expand struct App.Ctx.Quotes. Make sure the struct name is correct. If the struct name exists and is correct but it still cannot be found, you likely have cyclic module usage in your code
    (stdlib 3.11.2) lists.erl:1354: :lists.mapfoldl/3

The QuotesController generated by the mix phx.gen.html command is attempting to alias App.Ctx.Quotes on line 5 of the file:
https://github.com/dwyl/phoenix-content-negotiation-tutorial/blob/6f067cf8923e4fd481e3f7fc1a87a9c3d602a87d/lib/app_web/controllers/quotes_controller.ex#L4-L5

This context does not exist (and we don't want it to).

Expected behavior

We would expect that running mix phx.gen.html with the --no-context flag to create a controller without a context.

Actual behavior

The mix phx.gen.html creates a controller that attempts to alias a module that does not exist.

Check

Reading the tests for --no-context:

test "with --no-context skips context and schema file generation", config do in_tmp_project config.test, fn -> Gen.Html.run(~w(Blog Comment comments title:string --no-context)) refute_file "lib/phoenix/blog.ex" refute_file "lib/phoenix/blog/comment.ex"

We see that it merely refutes the existence of the "lib/phoenix/blog.ex" file (the Context in the test case) but it does not make any assertion as the content of the comments controller.

Feedback

I feel that the --no-context flag is insufficiently documented.
And consider the creation of a controller full of references to Ctx. (context namespace) to be a bug (or at least unexpected/undesirable behaviour). Running the mix phx.gen.* should not create code that does not compile.

If it's not possible to create a controller using mix phx.gen.* without a Context then the --no-context flag is miss-leading.

Environment Steps to Reproduce

Create a basic Phoenix App e.g:

mix phx.new app --no-ecto --no-webpack

Run the tests to confirm everything works.

You should see the following output in your terminal:

Generated app app
...

Finished in 0.02 seconds
3 tests, 0 failures

Attempt to create a Controller, View and Templates for a "Quotes" Resource without a Context:

mix phx.gen.html Ctx Quotes quotes author:string text:string --no-context --no-schema

That will create the files and it appears to all be good.
But now attempt to run the tests:

You will see a compilation error:

== Compilation error in file lib/app_web/controllers/quotes_controller.ex ==
** (CompileError) lib/app_web/controllers/quotes_controller.ex:13: App.Ctx.Quotes.__struct__/1 is undefined, cannot expand struct App.Ctx.Quotes. Make sure the struct name is correct. If the struct name exists and is correct but it still cannot be found, you likely have cyclic module usage in your code
    (stdlib 3.11.2) lists.erl:1354: :lists.mapfoldl/3

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4