Now save those changes and head back try to edit your List again. Your new action should now look like this: def new ( conn, _params ) do changeset = List. We need to update this changeset so it includes an empty Item by default. If we take a look at our new action in our ListController we can see where the changeset is coming from. So lets take a look at that changeset we are passing in and see what adjustments we need to make. If you take a closer look at the form you can see that it accepts a changeset as an argument. I had similar thoughts when I was trying to get this to work. You may be thinking, “But why Mike? I just did all this work updating my form, shouldn’t it show up”. We can see that even though we updated our form we still do not get an option to create a Item. Let’s head back to and try to create a new List. (If you are coming from a Rails background this should look similar to fields_for and accepts_nested_attributes_for.)Īdd the following to the your form. inputs_for allows us to attach nested data to the form. We are going to use inputs_for to build out our nested form. In order to do this we have to make a few changes to our form and controller.įirst go to web/templates/list/_. The is great, but we want to be able to create a new List and an Item at the same time from within the same form. You will see that we can click New List and we are taken to a form that allows us to create a new List. Run the following in your terminal: mix rverĪnd navigate to (Wow, doesn’t that look pretty! Phoenix comes preloaded with Bootstrap and so all of the generated code looks nice straight out of the box.) Now is a good time to fire up the server and check out what the generators have built for us so far. He does a great job walking you through how to build a simple Phoenix application without using generators. ![]() If you are new to Phoenix and are confused by the code that was generated I highly recommend getting Programming Phoenix by Chris McCord. Then we will run the migrations with mix ecto.migrate Now that we have our models setup lets get our database up and running. Item, on_delete: :delete_all timestamps () end. Web, :model schema " lists" do field :name, :string has_many :items, Todo. Open up web/models/List.ex and change it to look like below. There is one final step we have to take to finish this relation and that is to add has_many :items, Todo.Item, on_delete: :delete_all to the List model. This automatically will add belongs_to :list, Todo.List in your Item model and will create a list_id foreign key in your Item schema. This will ensure there is a relation between our models, making it so a List has many Items and a Item belongs to a List. This is almost identical to what we did with Lists but now we have added list_id:references:lists to the end. Open web/router.ex and add resources “items”, ItemController. We also need to add the routes for Items. In your terminal run: mix Item items body:string list_id:references:lists ![]() Now we need to scaffold out our Items resource. scope " /", Todo do pipe_through :browser # Use the default browser stack get " /", PageController, :index resources " /lists", ListController end. (… indicates truncated code) defmodule Todo. Your web/router.ex file should look like this. To fix this we need to open web/router.ex and add resources “lists", ListController. This generator created everything we need except for the proper routes. This is going to generate a model and the corresponding view, templates, and controller. Run this command in your terminal: mix List lists name:string We can now start to scaffold out the List resource to work with. To do this run the following commands in your terminal. Let’s go ahead and create a new application called todo. I decided to document what I learned in the process in hopes of helping others that are new to Elixir and Phoenix.įor this example we are going to build a basic Todo app. I am new to Phoenix and really struggled to find any resources that helped me with my specific problem. Nested forms are great when you want to create multiple database records in a single transaction and associate them with each other. I recently spent some time dealing with nested forms in Phoenix.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |