I have built a nested resources between question and answer model. This is form i used to create question, i just wrap info relevant:
<fieldset>
<legend>Question</legend>
<%= render 'new_question_fields', question_form: question_form %>
<legend>Answer</legend>
<%= question_form.simple_fields_for :answers do |answer_form| %>
<%= render 'answer', f: answer_form %>
<% end %>
</fieldset>
This is my answer partial:
<div class="answer_fields well fields">
<%= f.input :correct, label: 'This answer is correct.' %>
<%= f.input :content, input_html: { rows: 3, class: 'span6' } %>
</div>
This is my index page, display questions:
<ul class="questions">
<% @questions.each do |question| %>
<li><%= question.content %></li>
<ol class="answers">
<% question.answers.each do |answer| %>
<li><%= answer.content %></li>
<% end %>
</ol>
<% end %>
</ul>
On the page to create new question, I built 4 fields for 4 answer of question. This is html code of 4 textarea when they are render in html:
<textarea cols="40" id="question_answers_attributes_0_content" name="question[answers_attributes][0][content]" rows="3"></textarea>
<textarea cols="40" id="question_answers_attributes_1_content" name="question[answers_attributes][1][content]" rows="3"></textarea>
<textarea cols="40" id="question_answers_attributes_2_content" name="question[answers_attributes][2][content]" rows="3"></textarea>
<textarea cols="40" id="question_answers_attributes_3_content" name="question[answers_attributes][3][content]" rows="3"></textarea>
Say, i have order of fields is 0,1,2,3, but when i save questions, the orders of answer is reverse, example:
If i type 4 answers A,B,C,D corresponding the order of textarea on form is 0,1,2,3, when the question is saved, it display this: D,C,B,A, it means it saved value of textarea question[answers_attributes][3][content] first, then question[answers_attributes][2][content]…
Update: This is my index and create action in Question controller:
def index
@questions = Question.where("user_id = ?", current_user.id).paginate(page: params[:page], per_page: 10)
end
def create
@question = Question.new(params[:question])
@question.question_type_id = params[:question_type_id]
@question.user_id = current_user.id
if @question.save
flash[:success] = "Successfully created question."
redirect_to questions_url
else
render 'new'
end
end
My answer model:
class Answer < ActiveRecord::Base
attr_accessible :content, :question_id, :correct
belongs_to :question
end
what is happen when question is save? is this because method save of rails or my display form?
question.answers.order("created_at ASC")...or;question.answers.order("created_at DESC")...One of it should reverse order.
—
But more on that topic – I guess it’s about how stack is handled there. You are creating each answer by passing it as an attribute for question during its’ creation and that’s probably where’s the mix. Logs from Rails & database w/ log_level
:debugshould help to see how it goes into the database.Also
id ASCprobably works for you because your ids increment with time anyway and then it’s just the same ascreated_at ASC– but relying on ids may not always be a good approach since sometimes ids may be random.