benjamin.meichsner-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org
2007-Jan-10 12:11 UTC
forms contains collections ... of child-objects
hi all
in my app i have two models (and others of course). article and
article_price and article has many article_prices.
now i would like to create a view for editing all articles and its
current price in a table. when the the price is edited, a new price
with the current params should automatically be add.
the partial wasn''t the problem:
<tbody>
<% for @article in @articles %>
<% @price = @article.current_price %>
<tr class="<%= cycle(''even'',
''odd'') %>">
<td><label for="article_name">
<%= text_field( ''article[]'',
''name'', :size => 0)
%></label></td>
<td><label for="article_unit">
<%= text_field ''article[]'',
''unit'', :size => 5
%></label></td>
<td><label for="article_price_price">
<%= text_field_tag
"article_price[#{@article.id}][price]", (@price.price if @price),
:size
=> 5 %></label></td>
<td><label for="article_price_unit_quantity">
<%= text_field_tag
"article_price[#{@article.id}][unit_quantity]", (@price.unit_quantity
if @price), :size => 8 %></label></td>
<td><label for="article_price_order_number">
<%= text_field_tag
"article_price[#{@article.id}][order_number]", (@price.order_number if
@price), :size => 8 %></label></td>
[.......]
<% end %>
the user can edit every article an its current price in one row.
but the big question ist now, how can i access every article_price of
each article?
the normal solution for collection (update(params[:item].keys,
params[:item].values) doesn''t work here because i must integrate the
child-element and have to test if it was edited. the first idea of an
update action looks like this:
#i try to iterate over every price. the id isn''t the price id but the
id of the article, so i can identify the right article
params[:article_price].each do
|price|
article = Article.find(price.first) #first because of an array?
current_price = article.current_price
if !price[2][:price].empty?
new_price = ArticlePrice.new(:price => price[2][:price],
:unit_quantity => price[2][:unit_quantity],
:order_number => price[2][:order_number],
:start => Time.today)
article.add_price(new_price) #own method, for adding a price
end
end
[..]
but thats not working and of course not elegant.
i haven''t found any other problems like this, but i guess you know the
answer...
thanks a lot.
benni
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---