Forum: Ruby on Rails How do I create multiple records at once?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
deanholdren (Guest)
on 2005-11-14 21:30
(Received via mailing list)
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
Simon.Santoro (Guest)
on 2005-11-14 21:50
(Received via mailing list)
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'][]
deanholdren (Guest)
on 2005-11-15 00:20
(Received via mailing list)
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
Simon.Santoro (Guest)
on 2005-11-15 22:25
(Received via mailing list)
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?
deanholdren (Guest)
on 2005-11-16 18:38
(Received via mailing list)
my question was about creating multiple new records, not updating
existing
ones.
Jonas B. (Guest)
on 2005-11-17 01:44
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
Simon.Santoro (Guest)
on 2005-11-19 16:55
(Received via mailing list)
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
tankow (Guest)
on 2005-12-01 00:16
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
This topic is locked and can not be replied to.