* creating lib/hello_web/controllers/fallback_controller.ex * creating lib/hello_web/controllers/changeset_json.ex * creating lib/hello_web/controllers/post_json.ex $ mix Blog Post posts title:string content:string The module name we pass in must conform to the Elixir rules of module naming, following proper capitalization. The mix task takes the following arguments: the module name of the context, the module name of the schema, the resource name, and a list of column_name:type attributes. This command will not create any template for the app. Phoenix also offers the ability to generate all the code to stand up a complete JSON resource - Ecto migration, Ecto schema, controller with all the necessary actions and view. It will tell us we need to add a line to our router file, but since we skipped the schema, it won't mention anything about ecto.migrate. $ mix Blog Post posts body:string word_count:integer -no-schema Similarly, if we want a context created without a schema for our resource we can use the -no-schema flag. Add the resource to your browser scope in lib/hello_web/router.ex: It will tell us we need to add a line to our router file, but since we skipped the context, it won't mention anything about ecto.migrate. $ mix Blog Post posts body:string word_count:integer -no-context Note that this still requires a context module name as a parameter. If we don't want to create a context or schema for our resource we can use the -no-context flag. Lib/hello_web/controllers/post_controller.ex:22: /2 Warning: no route path for HelloWeb.Router matches \"/posts\" Important: If we don't do this, we will see the following warnings in our logs, and our application will error when compiling. Remember to update your repository by running migrations: When mix is done creating files, it helpfully tells us that we need to add a line to our router file as well as run our Ecto migrations. * injecting test/support/fixtures/blog_fixtures.ex * creating test/support/fixtures/blog_fixtures.ex * creating test/hello_web/controllers/post_controller_test.exs * creating lib/hello_web/controllers/post_html.ex * creating lib/hello_web/controllers/post_html/ * creating lib/hello_web/controllers/post_html/post_ * creating lib/hello_web/controllers/post_controller.ex $ mix Blog Post posts body:string word_count:integer Let's take a look at how to make this happen. Phoenix offers the ability to generate all the code to stand up a complete HTML resource - Ecto migration, Ecto context, controller with all the necessary actions, view, and templates. You can learn more about them or any other task by calling mix help TASK. We will cover all Phoenix Mix tasks, except phx.new,, and, which are part of the Phoenix installer. We have seen all of these at one point or another in the guides, but having all the information about them in one place seems like a good idea. Mix phx.server # Starts applications and their servers Mix # Creates a new Phoenix web project within an umbrella project Mix # Creates a new Ecto project within an umbrella project Mix phx.new # Creates a new Phoenix application Mix # Generates a Phoenix socket handler Mix # Generates an Ecto schema and migration file Mix # Generates a notifier that delivers emails by default Mix # Generates LiveView, templates, and context for a resource Mix # Generates controller, views, and context for a JSON resource Mix # Generates controller, views, and context for an HTML resource Mix # Generates an embedded Ecto schema file Mix # Generates a context with functions around an Ecto schema Mix # Generates a self-signed certificate for HTTPS testing Mix # Generates authentication logic for a resource Mix # Removes old versions of static assets. Mix phx.digest # Digests and compresses static files Mix phx # Prints Phoenix help information Mix local.phx # Updates the Phoenix project generator locally Note to learn more about mix, you can read Elixir's official Introduction to Mix. We can also create our own application specific tasks. There are currently a number of built-in Phoenix-specific and Ecto-specific Mix tasks available to us within a newly-generated application.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |