associating an array to multiple text fields in a form

Discussion in 'Ruby' started by robertlaferla@comcast.net, Jul 2, 2007.

  1. Guest

    I have a model that has the following classes: ShoppingList, Item and Product.

    -- *Product* is just a list of product names: Apple, Orange, Banana, Pear,
    Grape. The table has two columns: ID and NAME.

    -- A *ShoppingList* has a name (string) and many *Item* objects.

    -- Each *Item* has a *quantity* (integer) and to-one relationship to a
    *Product*.

    My ShoppingList's edit view has a form that contains a textfield for the name of
    a shopping list and a table of items and their quantities:

    Name: MyShoppingList

    Qty. Item
    3 Apples
    0 Oranges
    1 Pear

    The model object for this view is, of course, a ShoppingList.

    I am trying to bind each text field to the quantity attribute of each item in the shopping list. I tried something like this but it doesn't work because Rails wants an instance variable to bind to. How do I properly bind my array?

    <% @shopping_list.items.each do |item| %>
    <tr>
    <td><%= text_field("shopping_list.items[" + item.id.to_s + "].quantity",
    item.quantity, :size => 3) %></td><td><%= (Product.find
    item.product_name_id).name%></td>
    </tr>
    <% end %>
    , Jul 2, 2007
    #1
    1. Advertising

  2. I meant to post this to the Rails mailing list not this one. Sorry.
    Please don't respond to the list but rather to me personally.

    Thanks.
    Robert La Ferla, Jul 4, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vpcs
    Replies:
    0
    Views:
    1,545
  2. News
    Replies:
    4
    Views:
    392
    Eliyahu Goldin
    Jul 20, 2004
  3. JWL
    Replies:
    1
    Views:
    314
    Benjamin Niemann
    Oct 31, 2006
  4. Replies:
    0
    Views:
    275
  5. Replies:
    23
    Views:
    479
    Tim Streater
    Dec 18, 2013
Loading...

Share This Page