Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am trying to run through a simple nested_attributes exercise, where a question has many answers (how like life is that?). I can't get the answers field to show up in either the 'new' or the 'edit' actions. As far as I can tell, I'm following the specs precisely. Clearly, this is not the case. Any pointers appreciated, thanks.

Models

# question.rb
has_many :answers
accepts_nested_attributes_for :answers

# answer.rb
belongs_to :question

View

# _form.html.erb
<%= form_for(@question) do |f| %>
  ...
  <div class="field">
    <%= f.label :content, 'Question' %><br>
    <%= f.text_field :content %>
  </div>

  <div class="field">
        <% f.fields_for :answers do |ff| %>
        <%= ff.label :content, 'Answer' %><br>
        <%= ff.text_field :content %>
        <% end %>
  </div>
  ...
<% end %>

Controller

# questions_controller.rb
def new
  @question = Question.new
  @question.answers.build
end

def edit
end

def create
  @question = Question.new(question_params)

  respond_to do |format|
    if @question.save
      format.html { redirect_to @question, notice: 'Question was successfully created.' }
      format.json { render :show, status: :created, location: @question }
    else
      format.html { render :new }
      format.json { render json: @question.errors, status: :unprocessable_entity }
    end
  end
end

private 

def question_params
  params.require(:question).permit(:content, answer_attributes: [:id, :content, :question_id] )
end
share|improve this question

2 Answers 2

up vote 1 down vote accepted

Use <%=, not <%, otherwise the resulting output from the field_for Form Builder won't be output to the form.

<%= f.fields_for :answers do |ff| %>
share|improve this answer
    
That was it. Thank you. Now I just need to figure out why the nested attribute (the answer) isn't saving to the database. –  steel Jul 31 '14 at 17:58
2  
Probably because you are whitelisting answer_attributes instead of answers_attributes (NOTICE answers in plural) –  Kirti Thorat Jul 31 '14 at 18:00
1  
@Kirti, this is not the first time you've saved me. Happy to give you all of the upvotes, thank you. –  steel Jul 31 '14 at 18:06
    
@steel Glad to help :) –  Kirti Thorat Jul 31 '14 at 18:09

Now I just need to figure out why the nested attribute (the answer) isn't saving to the database.

Extending @Dylan Markow's answer.

Since it is has_many answers,it should be answers_attributes instead of answer_attributes in your question_params method.

def question_params
  params.require(:question).permit(:content, answers_attributes: [:id, :content, :question_id] )
end
share|improve this answer
    
Sweet nectar of the gods! This was the real problem I've been struggling with and you caught it. Now I need to figure out how to do it with a one-to-one. I can't accept your answer, as technically it didn't answer the question, but this made my day, thank you. –  steel Jul 31 '14 at 18:03
1  
@steel No Problem.Come up with your questions,we are here to help :) –  Pavan Jul 31 '14 at 18:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.