insert multiple records to a single table from a single form

Generally if we can create only 1 record, say 1 user at a time from the form. But what if I want to create multiple users from a single form? There are no associations with other models. How can i do that?

Answers


You have to make a form with an array of users params .e.g

<%= from_tag '/users/create_multiple' do %>
 <%= text_field_tag "users[][name]" %>
 <%= text_field_tag "users[][name]" %>
<% end %>

In UsersController:

def create_multiple
 params[:users].each do |user|
  user = User.create(user)
 end
end

You can add validation code as per your wishes, visit here how to pass form params for multiple records http://guides.rubyonrails.org/form_helpers.html#basic-structures


Need Your Help

Visual Basic C# windows form background worker object movement loop not functioning as intended

c# winforms visual-studio-2012 backgroundworker

I'm currently attempting to write a classic snake sort of game, where the user directs the snake around the screen using the keyboard's arrow keys. So far, i've managed to sort out the code that di...

How To Add Media To MediaStore on Android 4.4 KitKat SD Card With Metadata

android android-sdcard android-4.4-kitkat music-player android-mediascanner

Background: In Android 4.4, Google decided that apps should not have write access to SD cards.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.