0

I have a form that save a question and five answers in the database but I don't know how I can save the answers, this is my form:

<%= form_for([:admin, @question]) do |f| %>

...

<%= f.fields_for :answers do |builder| %>
    <%= builder.label :answer, "Risposta", :class => "v-align" %>
    <%= builder.text_field :answer, :rows => 2 %>

    <%= builder.label :correct, "Corretta", :class => "v-align" %>
    <%= builder.check_box :correct %>
<% end %>

...

<% end %>

My models:

class Question < ActiveRecord::Base
    has_many :answers
    accepts_nested_attributes_for :answers, :reject_if => lambda { |a| a[:content].blank? }, :allow_destroy => true
    attr_accessible :answers_attributes, :quiz_id, :question, :sort_order, :point_value, :number_correct, :explanation
end

class Answer < ActiveRecord::Base
    belongs_to :question
    attr_accessible :question_id, :answer, :correct, :sort_order
end

And my "Question" controller:

def new
    @question = Question.new
    5.times { @question.answers.build }

    respond_to do |format|
        format.html # new.html.erb
        format.json { render :json => @question }
    end
end

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

    respond_to do |format|
        if @question.save
            format.html { redirect_to admin_question_path(@question), :notice => 'Test was successfully created.' }
            format.json { render :json => @question, :status => :created, :location => @question }
        else
            format.html { render :action => "new" }
            format.json { render :json => @question.errors, :status => :unprocessable_entity }
        end
    end  
end

What I should do to save question and answer in the database?

Thanks!!

2 Answers 2

3

You only miss accepts_nested_attributes_for :answersin the Question model.

See doc.

EDIT:

You should add answers_attributes to your attr_accessible list

Sign up to request clarification or add additional context in comments.

7 Comments

Ops, I have it in the model but I miss it in this question... sorry!!
a mere @question.save should be ok. But proceed as done in scaffolding to handle errors. See guides.rubyonrails.org/getting_started.html#creating-new-posts
No, nothing... with @question.save I can save the question but I can't save the answers...
Did you set some attr_accessible?
yes but I still can't save answers... I think that I have to write something in the create action of question controller...
|
1

You should take a look at two RailsCasts:

http://railscasts.com/episodes/196-nested-model-form-part-1 and http://railscasts.com/episodes/197-nested-model-form-part-2

They might help you a lot!

The man behind those casts, Ryan Bates, created a great gem to handle nested forms!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.