A-laz
A-laz

Reputation: 55

Rails: fields_for iterating through list items for edit form

When i load the edit page i get the array printed out below the text fields for item. How do i iterate through @list.items without returning the array in the view?

<%= form_for @list, url: url_for(controller: 'inventory_lists', action: 'update') do |f| %>

Edit your list by updating Items

Any items not in your inventory list will incure additional fees

<%= @list.items.each do |item| %>
    <%= f.fields_for item, Item.find_by(id: item.id) do |item_attributes| %>
        <div>
            <%= item_attributes.label :name, "Item Name:" %>
            <%= item_attributes.text_field :name %>
        </div>
        <div>
            <%= item_attributes.label :room, "Room Item Belongs to:" %>
            <%= item_attributes.text_field :room %>
        </div>
        <div>
            <%= item_attributes.label :weight, "Item Weight:" %>
            <%= item_attributes.number_field :weight %>
        </div>
    <% end %>
<% end %>

<h3>Add Items</h3>
<div id="row1" class="row">
        <%= f.fields_for :item, Item.new do |item_attributes| %>
                <div>
                    <%= item_attributes.label :name, "Item Name:" %>
                    <%= item_attributes.text_field :name %>
                </div>
                <div>
                    <%= item_attributes.label :room, "Room Item Belongs to:" %>
                    <%= item_attributes.text_field :room %>
                </div>
                <div>
                    <%= item_attributes.label :weight, "Item Weight:" %>
                    <%= item_attributes.number_field :weight %>
                </div>
        <% end %>
</div>
<div>
    <input type="button" id="btnAdd" value="Add Another Item" />
    <input type="button" id="btnDel" value="Remove Item" />
</div>


<%= f.submit 'Update List' %>

<% end %>

this is the output below the @list.items.each do textfields

["\n \n <label for="inventory_list_item_name">Item Name:\n <input type="text" value="Couch" name="inventory_list[item][name]" id="inventory_list_item_name" />\n \n \n <label for="inventory_list_item_room">Room Item Belongs to:\n <input type="text" value="Living Room" name="inventory_list[item][room]" id="inventory_list_item_room" />\n \n \n <label for="inventory_list_item_weight">Item Weight:\n <input type="number" value="75" name="inventory_list[item][weight]" id="inventory_list_item_weight" />\n \n", "\n \n <label for="inventory_list_item_name">Item Name:\n <input type="text" value="Tv" name="inventory_list[item][name]" id="inventory_list_item_name" />\n \n \n <label for="inventory_list_item_room">Room Item Belongs to:\n <input type="text" value="Living Room" name="inventory_list[item][room]" id="inventory_list_item_room" />\n \n \n <label for="inventory_list_item_weight">Item Weight:\n <input type="number" value="40" name="inventory_list[item][weight]" id="inventory_list_item_weight" />\n \n", "\n \n <label for="inventory_list_item_name">Item Name:\n <input type="text" value="Cat Tree" name="inventory_list[item][name]" id="inventory_list_item_name" />\n \n \n <label for="inventory_list_item_room">Room Item Belongs to:\n <input type="text" value="Living Room" name="inventory_list[item][room]" id="inventory_list_item_room" />\n \n \n <label for="inventory_list_item_weight">Item Weight:\n <input type="number" value="10" name="inventory_list[item][weight]" id="inventory_list_item_weight" />\n \n"]

Upvotes: 1

Views: 176

Answers (1)

smehsan
smehsan

Reputation: 243

just correct this line from <%= @list.items.each do |item| %> to <% @list.items.each do |item| %> remove that = and it will go smoothly.

Upvotes: 2

Related Questions