I have an option to create users one by one with their names and email addresses as parameters:
<% provide(:title, 'Add User') %>
<h1>Add User</h1>
<div class="row">
<div class="col-md-4 col-md-offset-4">
<%= form_for(@user) do |f| %>
<%= render 'shared/user_error_messages' %>
<%= f.label :name %>
<%= f.text_field :name, class: 'form-control' %>
<%= f.label :email %>
<%= f.text_field :email, class: 'form-control'%>
<%= f.submit "Add User", class: "btn btn-primary" %>
<% end %>
</div>
</div>
What I wish to do is to have an option to create multiple users at a time. It should be a text area where you can enter names of users to be added. It should then parse the names and add "@domain.com" to create email addresses, as well, e.g. there's a Test Name, it should create a test.name@domain.com email address so that each user from bulk has a name and email address. There's no question in this part, just to be on the same page.
I have read a few threads on how to create multiple active records in rails, however, I am not sure how to pass the variables here and to make bulk create option work.
If any additional details are required, please do let me know.
Thank you in advance!
You can do this in your controller. First do what steve klein says, then you can do the following. For example if you want to use a text field to enter the new user names you can do something like in your form:
<%= f.label :user_names %>
<%= f.text_field :user_names_string, class: 'form-control' %>
And then access :user_names_string in your controller like this:
user_names_string = params[:user_names_string]
user_names = user_names_string.split(',').map(&:strip)
user_names.each do |user_name|
User.create(name: user_name, email: user_name + "@domain.com")
end
You could move some of this to the model, like the "@domain" part. You could also make new fields in your form per user, but this may get you on the right track.