I think that you need exactly what is described in these programs:
I think you also need to reorganize a bit, messages should not have questions. You may notice a slight difference from railway transmissions, but this is because you have only one answer to the question, while in the rails there are many answers to the question. Part 2 shows how to add AJAX calls to add / remove questions and answers (you may not need this if you only have one answer).
Mandatory reading so that you better understand associations and how nested attributes work:
And this is an example that is likely to work with minimal setup. I did not use semantic fields, just a standard form constructor.
class Post < ActiveRecord::Base has_many :questions accepts_nested_attributes_for :questions, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true end class Question < ActiveRecord::Base belongs_to :post has_one :answer, :dependent => :destroy accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true end class Answer < ActiveRecord::Base belongs_to :question end # posts_controller.rb def new @post = Post.new # lets add 2 questions 2.times do question = @post.questions.build question.build_answer respond_to do |format| format.html end end # views/posts/_form.html.erb <%= form_for @post do |f| %> <%= f.error_messages %> <p> <%= f.label :name %><br /> <%= f.text_field :name %> </p> <%= f.fields_for :questions do |builder| %> <%= render "question_fields", :f => builder %> <% end %> <p><%= f.submit "Submit" %></p> <% end %> # views/posts/_question_fields.html.erb <p> <%= f.label :content, "Question" %><br /> <%= f.text_area :content, :rows => 3 %><br /> <%= f.check_box :_destroy %> <%= f.label :_destroy, "Remove Question" %> </p> <%= f.fields_for :answers do |builder| %> <%= render 'answer_fields', :f => builder %> <% end %> # views/posts/_answer_fields.html.erb <p> <%= f.label :content, "Answer" %> <%= f.text_field :content %> <%= f.check_box :_destroy %> <%= f.label :_destroy, "Remove" %> </p>
source share