Forum: Ruby on Rails Form with multiple rows of content

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Nikhilesh (Guest)
on 2006-04-24 11:27
I have looked at examples for this. I found a similar example in the
Agile Web Dev... book but that didn't help. Here's what i am trying to
do.

A Cart has many line items. On the Display Cart page, I want to provide
the user the option to update quantities.

Here's the rhtml code:
    <%= start_form_tag :action => 'update_cart'  %>
    <% for @item in @items %>
   	    <%= @item.product.title %>
            <%= text_field "item[]",'quantity' %>
	    <%= number_to_currency(@item.unit_price) %>
	    $<%= @item.quantity * @item.unit_price %>
	   <%= link_to 'Remove item', {:action => 'remove_from_cart', :id =>
@item.product}  %>
     <% end %>
       Total
       $ <%= @cart.total_price %>
  <%= submit_tag 'Update Cart' %>
  <%= end_form_tag %>

In the Page->View source I find that all text boxes have the name
item[quantity]. I expected the names to be item[0][quantity],
item[1][quantity] and so on.
And instead of passing all the rows as params, only the first row (i.e.
first line item) is being passed to the update_cart function.

I've spent 3 days on this. Please help !!
Hemant K. (Guest)
on 2006-04-24 17:27
(Received via mailing list)
Though i am having similar kind of problem....but there is a option
called
index or something...using which you can index your keys. But for
me...real
problem lies...within the fact that....ideally <%= file_field :picture
"image" %>, here image should be a method within the model
class...now....if
i have a array of images...then how can i assign same key to all of
them?
Hemant K. (Guest)
on 2006-04-25 02:24
(Received via mailing list)
yeah its really simple:

<%= text_field "item", "quantity", :index => @item.unit_price %>
Guest (Guest)
on 2006-04-27 00:52
hemant kumar wrote:
> yeah its really simple:
>
> <%= text_field "item", "quantity", :index => @item.unit_price %>

Thanks.
If i do this then the parameters passed will be
item[0]=>quantity=><i>n1</i>
item[1]=>quantity=><i>n2</i>
item[2]=>quantity=><i>n3</i>

and so on...

How do i read this at the server? Now this becomes a Ruby language
question actually and not a Rails question.

This is just an enhancement to the Depot application in the Agile Web
Dev book. The objective is to update all the quantities of all the line
items of the cart.
This topic is locked and can not be replied to.