You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
** (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:
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
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
Elixir version (elixir -v): Elixir 1.10.1 (compiled with Erlang/OTP 22)
Phoenix version (mix deps): Phoenix v1.5.1 (mix phx.new -v)
NodeJS version (node -v): v12.16.2
NPM version (npm -v): 6.14.4
Operating system: OSX 10.15.4
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.
mix test
You should see the following output in your terminal:
That will create the files and it appears to all be good.
But now attempt to run the tests:
mix test
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
The text was updated successfully, but these errors were encountered:
nelsonic
referenced
this issue
in dwyl/phoenix-content-negotiation-tutorial
May 11, 2020
In some cases, you may wish to bootstrap HTML templates, controllers, and controller tests, but leave internal implementation of the context or schema to yourself.
In this case, we leave the context and schema implementations up to you, but it is still up to you to define them. The generators are first and foremost learning tools, so we have no plans to support a set of generators that puts the code directly in the controller, but you are free to write an external library that fit your generator needs. Thanks!
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
The command I've have attempted to run is:
But see the following error:
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:And that appears to work in that we see the following output:
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:
The
QuotesController
generated by themix phx.gen.html
command is attempting toalias 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
:phoenix/test/mix/tasks/phx.gen.html_test.exs
Lines 234 to 239 in f0049f0
We see that it merely refutes the existence of the
"lib/phoenix/blog.ex"
file (theContext
in the test case) but it does not make any assertion as the content of thecomments
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 themix 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
Elixir 1.10.1 (compiled with Erlang/OTP 22)
Phoenix v1.5.1
(mix phx.new -v
)v12.16.2
6.14.4
10.15.4
Steps to Reproduce
Create a basic Phoenix App e.g:
Run the tests to confirm everything works.
You should see the following output in your terminal:
Attempt to create a Controller, View and Templates for a "Quotes" Resource without a Context:
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:
The text was updated successfully, but these errors were encountered: