1

I have a survey app that allows people to create questions. When a user creates a new question they can also supply answer options. For Ex. Question: What color is the sky? Answer Options: Blue, Red, Purple.

Right now, my single form allows for the creation of the questions model, and options model, but I want to create multiple options from the same form.

Should I create a unbounded form_tag that allows a user to submit an array of hashes, and iterate over that array to create each option model record? Or is there another way of doing this?

option.rb

class Option < ActiveRecord::Base
  belongs_to :question
end

question.rb

class Question < ActiveRecord::Base
  has_many :options
  accepts_nested_attributes_for :options              
end

Questions Controller

class QuestionsController < ApplicationController
  def new
    @question = Question.new
    @question.options.build
  end

  def create
    @question = Question.new(question_params)
    if @question.save
      redirect_to @question
    else
      render 'new'
    end
 end

 private
    def question_params
      params.require(:question).permit(:title, :desc, options_attributes:[:id, :scope, :option, :question_id])
    end
end

_form.html.erb

<%= form_for @question do |f| %>
    <%= f.text_field :question, class: "form-control", placeholder: "Survey Question?" %>
    <%= f.fields_for :options do |u| %>
        <%= u.text_field :option, class: "form-control", placeholder: "Answer Option 1", id: "answer"%>
        <%= u.text_field :option, class: "form-control", placeholder: "Answer Option 2", id: "answer"%>
        <%= u.text_field :option, class: "form-control", placeholder: "Answer Option 3", id: "answer"%>
    <% end %>   
<%= f.submit 'Submit', class: "btn btn-default" %>
thedeepfield
  • 6,138
  • 25
  • 72
  • 107

1 Answers1

0

It looks like you are trying to create 3 options as answers, you just need to update the new action to something like:

def new
  @question = Question.new
  3.times { @question.options.build }
end

This way, the new view will render 3 options as you might expect, but you will have to slightly change the _form.html.erb file like so:

<%= form_for @question do |f| %>
  <%= f.text_field :question, class: "form-control", placeholder: "Survey Question?" %>
  <%= f.fields_for :options do |u| %>
    <%= u.text_field :option, class: "form-control", placeholder: "Answer Option" %>
  <% end %>   
  <%= f.submit 'Submit', class: "btn btn-default" %>
<% end %>

I'm assuming you won't need the ids for the fields, as they were repeated on each input, but I think it is related to a copy/paste misunderstanding...

Also if you need this to be dynamically, check out the nested_form as suggested by @Richlewis

Good luck!

kurenn
  • 1,035
  • 9
  • 11
  • 1
    is nested_forms a gem? While your solution works, I don't know how many answer options the user is going to create and I don't want to restrict to just 3. I worked around by having javascript create multiple text_field_tags. When the user submits, When the user submits, I pass each field as an array iterate over it to create multiple answer options. However this feels pretty messy. – thedeepfield Dec 16 '14 at 06:50
  • Yeah the nested_form is a gem from ryan bates, check it out a github, [https://github.com/ryanb/nested_form](https://github.com/ryanb/nested_form) and here are the railscasts for you to try [http://railscasts.com/episodes/196-nested-model-form-part-1](http://railscasts.com/episodes/196-nested-model-form-part-1), [http://railscasts.com/episodes/197-nested-model-form-part-2](http://railscasts.com/episodes/197-nested-model-form-part-2)... – kurenn Dec 16 '14 at 17:20