Trying to follow your example gives this (more or less) on update:
def update
@page = Page.find(params[:id])
params[:content_item].each { |key, item|
@page.content_items[key.to_i].attributes = item
@page.content_items[key.to_i].save # <= disturbing
}
@page.attributes = params[:page]
if @page.save
flash[:notice] = ''Page was successfully updated.''
redirect_to :action => ''show'', :id => @page
else
render :action => ''edit_content''
end
end
I''ve tried to loop through all content_items and just update their
attributes without doing the explicit save of each item. But, if I
don''t
do the save it won''t get updated when I do the @page.save.
Isn''t a save
to @page supposed to cascade to a has_many collection? Doing as I do now
I end up with lots of updates sent to the database (unless ActiveRecord
utilizes some smart write behind etc...) and that doesn''t feel very
good.
/Marcus
-----Original Message-----
From: Wilson [mailto:defiler-ifvz4xmYPRU@public.gmane.org]
Sent: den 18 juli 2005 20:30
To: Marcus Andersson
Subject: Re: [Rails] Dynamically generate form fields
Our e-mails probably hit the list at almost the same moment, but check
out the example code I pasted up here:
http://supremetyrant.com/misc/rails/
new_members.rhtml and its partial form _member.rhtml do what you''re
describing, with an arbitrary number of indexed form fields via a
partial.
Marcus Andersson wrote:> I have a model with an array of items (has_many). I want to generate
> form fields (for example a text area) for each of these items. Say
> that each item has an attribute with the name "name" to be used
as
> field title and that the attribute named "content" is the one to
be
> edited (for each item).
>
> Is it possible to do with form helpers (and a loop over the items I
> guess) and is update_attributes able to take care of the updating of
> that kind of form? If possible, how? If not possible, what is the
> Rails way of doing this? Can I use partials in some clever way?
>
> /Marcus
>