I''m trying to handle a form with multiple model objects. Since english is not my first language I will let the code do the talking :) <%= start_form_tag :action => ''describe'' %> <% for @photo in @todescribe %> <p><label for="photo_title">Photo Title</label><br/> <%= text_field ''photo'', ''title'' %></p> <p><label for="photo_description">Photo Description</label><br/> <%= text_area ''photo'', ''description'', :rows => 7 %></p> <p><label for="photo_tags">Categories</label><br/> <%= text_field ''photo'', ''tags'' %></p> <%= hidden_field ''photo'', ''id'' %> <% end -%> <%= submit_tag "Update" %> <%= end_form_tag %> I have Photo model instances in @todescribe, and I loop over them in this view. However, all of them have the same field names (so I loose parameters in my controller). How can I fix this? And what is the best format: photo[title][1] or photo[1][title]? Can rails handle this? A second question: What about error messages: Right now I would have to check it like this: if not @photo.errors.is_empty? error_messages_on ''photo'', ''field'' end for each field and object.. Is there a better way? I don''t like to see five different error instance boxes like I would if i used error_messages_for for each object. Thanks!
François Beausoleil
2005-Sep-06 14:35 UTC
Re: multiple instances of model class in 1 form?
Hello Bart, Bart Bosman said the following on 2005-09-06 09:56:> I''m trying to handle a form with multiple model objects. Since english > is not my first language I will let the code do the talking :) > > <%= start_form_tag :action => ''describe'' %> > > <% for @photo in @todescribe %> > <p><label for="photo_title">Photo Title</label><br/> > <%= text_field ''photo'', ''title'' %></p> > > <p><label for="photo_description">Photo Description</label><br/> > <%= text_area ''photo'', ''description'', :rows => 7 %></p> > > <p><label for="photo_tags">Categories</label><br/> > <%= text_field ''photo'', ''tags'' %></p> > > <%= hidden_field ''photo'', ''id'' %> > <% end -%> > > <%= submit_tag "Update" %> > > <%= end_form_tag %> > > I have Photo model instances in @todescribe, and I loop over them in > this view. However, all of them have the same field names (so I loose > parameters in my controller). How can I fix this? > > And what is the best format: photo[title][1] or photo[1][title]? Can > rails handle this?Yup, there is: <p><label for="photo[<%= @photo.id %>]_title">Photo Title</label><br/> <%= text_field ''photo[]'', ''title'' %></p> I know the text_field will work, you might have to check the actual format to know what to name your label. In your controller, you''ll receive the Photos in this format: photo 1 title description 2 title description and so on. You will probably want to iterate over all instances like this: params[:photo].each_pair do |id, values| photo = Photo.find(id) photo.update_attributes(values) end> A second question: > > What about error messages: > > Right now I would have to check it like this: > > if not @photo.errors.is_empty? > error_messages_on ''photo'', ''field'' > end > > for each field and object.. Is there a better way? I don''t like to see > five different error instance boxes like I would if i used > error_messages_for for each object.Now that one, I''ll let another more experienced developer answer... You could always use a partial, no ? Bye ! François