How to lay two resource variables in a form?

I am trying to make a dynamic form of questions and answers, for example:

Question _______

Reply _______

Question _______

Reply _______

I cannot figure out how to iterate over two resources as alternating pairs. I tried this:

<%= semantic_fields_for [@question, @answer] do |h, i| %> <%= f.inputs :for => @question do |h|%> <%= h.input :question %> <% end %> <%= f.inputs :for => @answer do |i|%> <%= i.input :answer %> <% end %> <% end %> 

But this gives me the error "Undefined method` model_name 'for Array: Class."

My controller:

 def new @post = Post.new @question = @post.questions.new @answer = @question.build_answer respond_to do |format| format.html end end 

And my models:

 class Post < ActiveRecord::Base has_many :questions has_many :answers end class Question < ActiveRecord::Base belongs_to :post has_one :answer end class Answer < ActiveRecord::Base belongs_to :question belongs_to :post end 
0
source share
2 answers

Therefore, I personally do not use formtastic, but I understand that it is similar to simple_form strings. Your error comes from trying to pass an array to semantic_fields_for, which accepts only one object:

 <%= semantic_form_for @questions do |q| %> <%= q.input :question %> <%= q.semantic_fields_for @answer do |a| %> <%= a.inputs :answer %> <% end %> <%= q.actions %> <% end %> 

Remember that your models must be properly configured with accepts_nested_attributes_for

 class Question < ActiveRecord::Base belongs_to :post has_one :answer accepts_nested_attributes_for :answers end 

Do you want to check format documents at https://github.com/justinfrench/formtastic

This should correctly display your form in the view, but you need to add a few more questions to the question controller to make sure it saves the answers (someone corrects me if I am wrong).

Also, is it so clear that your question and answer tables really contain a column of questions and answers? If the columns are actually something like: body, you will want to replace the corresponding characters in the above code.

0
source

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> 
0
source

All Articles