What would be the best way to

I have a model ‘assignment’ that has_many of the model ‘answer’. What
would be the best way to have a form to save an ‘assignment’ and some
of its ‘answers’ all on the same form?

Brandon wrote:

I have a model ‘assignment’ that has_many of the model ‘answer’. What
would be the best way to have a form to save an ‘assignment’ and some
of its ‘answers’ all on the same form?

given both models have three attributes, title, content & brief;
i believe would this work (untested code)

text_field ‘assignment’, ‘title’
text_field ‘assignment’, ‘content’
text_field ‘assignment’, ‘brief’
text_field ‘assignment’, ‘answer[title]’
text_field ‘assignment’, ‘answer[content]’
text_field ‘assignment’, ‘answer[brief]’

On Aug 6, 2:37 am, Shai R. [email protected]
wrote:

given both models have three attributes, title, content & brief;
i believe would this work (untested code)

text_field ‘assignment’, ‘title’
text_field ‘assignment’, ‘content’
text_field ‘assignment’, ‘brief’
text_field ‘assignment’, ‘answer[title]’
text_field ‘assignment’, ‘answer[content]’
text_field ‘assignment’, ‘answer[brief]’

Hmm… that didn’t work. Also, I need to have more than 1 answer on
the form. Maybe something like text fields with
id=“assignment[answer][1][content]”?

Hi –

On Mon, 6 Aug 2007, Brandon wrote:

text_field ‘assignment’, ‘brief’
text_field ‘assignment’, ‘answer[title]’
text_field ‘assignment’, ‘answer[content]’
text_field ‘assignment’, ‘answer[brief]’

Hmm… that didn’t work. Also, I need to have more than 1 answer on
the form. Maybe something like text fields with
id=“assignment[answer][1][content]”?

You can give an arbitrary index to text fields, like this:

<% @assignment.answers.each_with_index do |answer,i| %>
<%= text_field “answer”, “title”, :index => i %>

<% end %>

You’ll then get params[:answer], which will be a hash with keys
corresponding to the indices.

Maybe that, or something based on it, would give you what you need.

David