Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6164663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T22:01:56+00:00 2026-05-23T22:01:56+00:00

I use accepts_nested_attributes_for in a model but my child form isn’t saved in the

  • 0

I use accepts_nested_attributes_for in a model but my child form isn’t saved in the database?

I am building a nested form almost in the same way as in episode 196 / 197 from Ryan Bates railscasts. I have a parent question form and as a child the answer form:

models/question.rb

class Question < ActiveRecord::Base
  belongs_to :user
  has_many :answers, :dependent => :destroy
  accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:text].blank? }, 
                                :allow_destroy => true

  validates :content, :presence => true
end

models/answer.rb

class Answer < ActiveRecord::Base
    belongs_to :question
  belongs_to :user
end

controllers/questions_controller.rb

class QuestionsController < ApplicationController

  before_filter :authenticate_user!

  def index
    setup_questions
  end

  def create    
    @question = Question.new(params[:question])

    #this is to get every id_key from the user into the params of the answer
    params[:question][:answers_attributes].keys.each {|key| params[:question][:answers_attributes][key][:user_id] = current_user.id }

    @question.user_id = current_user.id

    if @question.save
      redirect_to questions_path, :notice => "Successfully created question."
    else
      setup_questions
      render :index
    end
  end

  def edit
        @question = Question.find(params[:id])  
  end

  def update
     @question = Question.find(params[:id])
      respond_to do |format|
        if @question.update_attributes(params[:question])
          format.html { redirect_to(@question, :notice => 'Question was successfully updated.') }
          format.xml  { head :ok }
        else
          format.html { render :action => "edit" }
          format.xml  { render :xml => @question.errors, :status => :unprocessable_entity }
        end
      end 
  end

  def show
    @question = Question.find(params[:id])
  end

  def destroy
    @question = Question.find(params[:id])
    #authorize! :destroy, @question
    @question.destroy
    redirect_to questions_path, :notice => "Successfully deleted question: #{@question.content}."      
  end


  private

  def setup_questions
    @questions = Question.all
    @question ||= Question.new
    @question.answers.build #to build the answers form
  end
end

views/question/_form.html.erb

<%= form_for(@question) do |f| %>

<!-- =================== -->
<!-- = Error handeling = -->
  <% if @question.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@question.errors.count, "error") %> prohibited this question from being saved:</h2>

      <ul>
      <% @question.errors.full_messages.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    </div>
  <% end %>
<!-- =================== -->

<!-- ================= -->
<!-- = Question form = -->
  <div class="field">
    <%= f.label :content, "Question" %><br />
    <%= f.text_field :content, :placeholder => "type your question here.." %>
  </div>
<!-- ====================== -->

<!-- = Nested Answer form = -->
    <div class="answer-field">
        <%= f.fields_for :answers do |builder| %>
            <%= builder.label :content, "Possible answer" %><br />
            <%= builder.text_field :content, :placeholder => "type an optional answer.." %>
        <% end %>
    </div>
<!-- ====================== -->

  <div class="actions">
    <%= f.submit %>
  </div>

<!-- ================= -->
<% end %>

So now the big question… Why doesn’t it save anything from the answer field to the database? I thought one create action at the parent (question) should be enough and that the “accepts_nested_attributes_for” should take care of it’s child(s).

Regards,
Thijs

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T22:01:57+00:00Added an answer on May 23, 2026 at 10:01 pm

    1 Remove this line from controller

    params[:question][:answers_attributes].keys.each {|key| params[:question][:answers_attributes][key][:user_id] = current_user.id }
    

    2 add this into your view

    <!-- = Nested Answer form = -->
      <div class="answer-field">
        <%= f.fields_for :answers do |builder| %>
          <% builder.object.user = current_user %>
          <%= builder.label :content, "Possible answer" %><br />
          <%= builder.text_field :content, :placeholder => "type an optional answer.." %>
          <%= builder.hidden_field, :user_id %>
        <% end %>
      </div>
    <!-- ====================== -->
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to use a nested form but keep getting this error when
attempting to use this railscast as a guide: http://railscasts.com/episodes/197-nested-model-form-part-2?view=asciicast and running into this error:
I am trying to set up a nested model form similar to the one
I'm trying to use rails nested form_for helper, but I am getting the following
I'm trying to use accepts_nested_attributes_for on a has_one association model, and getting absolutely nowhere
I have a few checkboxes coming from nested model ( :admin accepts_nested_attributes_for :account_setting ).
use this website a lot but first time posting. My program creates a number
I have a complex form for a model Schedule that belongs to a parent
So I have model with an association and I'm using a single form with
I have this Pin Model: class Pin < ActiveRecord::Base belongs_to :user belongs_to :image accepts_nested_attributes_for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.