Add Multiple Records of a model using single form

hello all,

I am working on a simple project on ruby on rails. I need to add a data to the database. I would like to add multiple records of a model using single form and single submit button so that all the records I entered in the form are inserted into database table. I need to design a form with multiple duplicate fields, each set of fields represent a database record.

                Thanks in Advance

something like that:
<% 1.upto(2) do |i| %>
<%= text_field_tag “fields[#{i}][user_name]”,’’, :class => “user_name” %>
<%= radio_button_tag “fields[#{i}][is_checked]”, ‘1’, false %>

<% end %>
params[:fields].each do |i, values|
u = User.create(values)
end

Thank you @Werner its Working.

Thank you @Werner its Working.

something like that:

*<% 1.upto(2) do |i| %>
<%= text_field_tag "fields[#{i}][user_name]",'', :class => "user_name" %>
<%= radio_button_tag "fields[#{i}][is_checked]", '1', false %><br>
<% end %>

params[:fields].each do |i, values|
u = User.create(values)
end*

Just for clarification, you don't need to supply an index

<% 2.times do %>
  <%= text_field_tag "fields[][user_name]",'', :class => "user_name" %>
  <%= radio_button_tag "fields[][is_checked]", '1', false %><br>
<% end %>

should be enough :slight_smile:

params[:fields] will be an array of attributes instead of a hash
so you can do User.create params[:fields] (I think :D)