Validating collections


#1

I saw this issue addressed in mid-January under the
heading ‘TIP: Using field_error_proc to add style
attributes to form elements’, but I didn’t quite get
it.

I have a collection of objects that I want to input in
one form and validate (during update). The suggestion
in the post I referred to above was to use something
like
[in form]

<% for @comment in @post.comments %>
<%= text_field ‘comment[]’, ‘author’ %>
<%= text_field ‘comment[]’, ‘body’ %>
<% end %>
[in controller]
all_valid = @post.comments.all? { |c| c.valid? }
or
all_valid = @post.comments.inject(true) {|memo, c|
c.valid? && memo }
but I’m not sure how @post.comments gets filled. I
mean, I get how to send @post to the view, but when I
return to the controller for validation, what is
@post? And how does it relate to the data that was
passed in the form?

Thanks,
Brian Gates


Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com