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
–
- Books:
RAILS ROUTING (new! http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (Ruby for Rails) - Ruby/Rails training
& consulting: Ruby Power and Light, LLC (http://www.rubypal.com)