Skip to content

Instantly share code, notes, and snippets.

@marklocklear
Created September 25, 2011 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marklocklear/1240110 to your computer and use it in GitHub Desktop.
Save marklocklear/1240110 to your computer and use it in GitHub Desktop.
#controller
def new
@questions = Array.new(3) {Question.new}
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @question }
end
end
def create
@questions = params[:questions].values.collect { |question| Question.new(question) }
if @questions.all?(&:valid?)
@questions.each(&:save!)
redirect_to :action => 'index'
else
render :action => 'new'
end
end
#view
<%= form_tag :action => 'create' %>
<% @questions.each_with_index do |question, index| %>
<%= fields_for "questions[#{index}]", question do |f| %>
Name: <%= f.text_field :name %>
<%= f.submit %>
<% end %>
<% end %>
<%= form_tag %>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment