How do I create multiple records at once?


#1

I’d like to create multiple records at once, but I’m having trouble with
text_field helper tag.
if i have the following within a form:

<%= text_field ‘contact’, ‘name’ %><%= text_field ‘contact’, ‘number’ %>
<%= text_field ‘contact’, ‘name’ %><%= text_field ‘contact’, ‘number’ %>

I would like to somehow get an array of contacts in my params like:

contact => [ {name => “foo”, number => “1234”}, {name => “bar”, number
=>
"9876} ]

is there anyway with the naming convention to get this to work with the
text_field tag? or at the least a regular input tag?
I tried the following, but it didn’t work:

<%= text_field ‘contact[1]’, ‘name’ %><%= text_field ‘contact[1]’,
‘number’
%>
<%= text_field ‘contact[2]’, ‘name’ %><%= text_field ‘contact[2]’,
‘number’
%>

I recieved the following error:

`@contact[1]’ is not allowed as an instance variable name


#2

Dean H. wrote:

=> "9876} ]
just do it like that. you will have an array in your controller that you
can access with params[:contact][‘name’][] and
params[:contact][‘number’][]


#3

It doesn’t work for me. If I do it like:

<%= text_field ‘contact’, ‘name’ %><%= text_field ‘contact’, ‘number’ %>
<%= text_field ‘contact’, ‘name’ %><%= text_field ‘contact’, ‘number’ %>

I get the following in my parameters:

Parameters: {“commit”=>“Create”, “action”=>“create”,
“controller”=>“my_controller”, "
contact"=>{“name”=>“name1”, “number”=>“1111”}}

there is only one “contact”, not an array of contacts as you say.

As a workaround, I’m doing:
<%= text_field ‘contact_1’, ‘name’ %><%= text_field ‘contact_1’,
‘number’ %>
<%= text_field ‘contact_2’, ‘name’ %><%= text_field ‘contact_2’,
‘number’ %>

but I’d rather not have to name them like that, and have an array of
“contact” or at least two different arrays as you stated of “name” and
“number” within a “contact” param


#4

Dean H. wrote:

there is only one “contact”, not an array of contacts as you say.

<%= start_form_tag :action => ‘update_movies’ %>

<% for movie in @movies %>

<%= hidden_field_tag “movie[id][]”, movie.id %>
<%= text_field_tag “movie[title][]”, movie.title %>
<%= text_field_tag “movie[description][]”, movie.description %>

<% end %>

<%= submit_tag ‘Save’ %>
<%= end_form_tag %>

this gets me an array in the update_movies action:
@movies = params[:movie]

did you try?


#5

my question was about creating multiple new records, not updating
existing
ones.


#6

Hi!

Can’t you create a temporary @contact instance variable as an array of
contacts and in the form do:
<%= text_field ‘contact[]’, ‘name’ %><%= text_field ‘contact[]’,
‘number’ %>
<%= text_field ‘contact[]’, ‘name’ %><%= text_field ‘contact[]’,
‘number’ %>

/Jonas

deanholdren wrote:

I’d like to create multiple records at once, but I’m having trouble with
text_field helper tag.
if i have the following within a form:

<%= text_field ‘contact’, ‘name’ %><%= text_field ‘contact’, ‘number’ %>
<%= text_field ‘contact’, ‘name’ %><%= text_field ‘contact’, ‘number’ %>

I would like to somehow get an array of contacts in my params like:

contact => [ {name => “foo”, number => “1234”}, {name => “bar”, number
=>
"9876} ]

is there anyway with the naming convention to get this to work with the
text_field tag? or at the least a regular input tag?
I tried the following, but it didn’t work:

<%= text_field ‘contact[1]’, ‘name’ %><%= text_field ‘contact[1]’,
‘number’
%>
<%= text_field ‘contact[2]’, ‘name’ %><%= text_field ‘contact[2]’,
‘number’
%>

I recieved the following error:

`@contact[1]’ is not allowed as an instance variable name


#7

Can we get an example of this? With the controllers and views required
to do this?

Thanks

Simon.Santoro wrote:

Dean H. wrote:

my question was about creating multiple new records, not updating
existing ones.

You sad you “would like to somehow get an array of contacts in my
params”. And thats the way to do it: call the fields in your form like
this:

<%= hidden_field_tag “movie[id][]”, movie.id %>
<%= text_field_tag “movie[title][]”, movie.title %>
<%= text_field_tag “movie[description][]”, movie.description %>

<%= hidden_field_tag “movie[id][]”, movie.id %>
<%= text_field_tag “movie[title][]”, movie.title %>
<%= text_field_tag “movie[description][]”, movie.description %>

<%= hidden_field_tag “movie[id][]”, movie.id %>
<%= text_field_tag “movie[title][]”, movie.title %>
<%= text_field_tag “movie[description][]”, movie.description %>

and you will get an array in your controller that you can access.

ps.
please quote my message correctly next time


#8

Dean H. wrote:

my question was about creating multiple new records, not updating
existing ones.

You sad you “would like to somehow get an array of contacts in my
params”. And thats the way to do it: call the fields in your form like
this:

<%= hidden_field_tag “movie[id][]”, movie.id %>
<%= text_field_tag “movie[title][]”, movie.title %>
<%= text_field_tag “movie[description][]”, movie.description %>

<%= hidden_field_tag “movie[id][]”, movie.id %>
<%= text_field_tag “movie[title][]”, movie.title %>
<%= text_field_tag “movie[description][]”, movie.description %>

<%= hidden_field_tag “movie[id][]”, movie.id %>
<%= text_field_tag “movie[title][]”, movie.title %>
<%= text_field_tag “movie[description][]”, movie.description %>

and you will get an array in your controller that you can access.

ps.
please quote my message correctly next time