Inserting multiple rows in the table at same time

Hi all,

I have to implement the functionality where I have to insert multiple
rows at the same time in the table. The functionality is implemented but
it is taking only the first row multiple times.

The view is:

Quantity <% @units.each do |unit| %>

<% end %>

The controller is:

def add_price
@product=Product.find(params[“id”])
@units = Unit.find :all
@length = @units.length
@length.times do
@productprice = Productprice.new(params[:productprice])
if @productprice.save
end
end
end

Please give me the solution of the above problem.

Thanks and Regards,
Rohit.

<input id = “quantity” name=“productprice[quantity]”…

maybe, all your inputs get the same id and name and that’s the source of
trouble? especially if the enclosing form has autocomplete on…

try something like id=“quantity_#{unit.id}”

you’ll need different id’s anyway, to get the right data in the
receiving controller

Thorsten M. wrote:

<input id = “quantity” name=“productprice[quantity]”…

maybe, all your inputs get the same id and name and that’s the source of
trouble? especially if the enclosing form has autocomplete on…

try something like id=“quantity_#{unit.id}”

you’ll need different id’s anyway, to get the right data in the
receiving controller

Yeah you r rite. All the inputs had the same id and name.

Now my view looks like:
<% i = 0 %>
<% @units.each do |unit| %>
<% i = i+1 %>

_i" name="productprice[quantity]" type="text" value="<%= unit.units%>">

<% end %>
Now the ids generated are quantity1,price1 and quantity2, price2.

But am not able to access these in my controller.

Please help me out.

Thanks,
Rohit.

this depend on some details, mainly how the form is created and named

normaly you would get your params like params[:form_name][:field_name]
so if you have something like form_for :units

then
params[:units][:quantity_1]

should give you a result

otherwise look in you development.log. after submitting your form it
should look something like this:
(the Parameters part is of interest)

Processing UsersController#create (for 127.0.0.1 at 2007-11-26 12:08:32)
[POST]
Session ID: Parameters: {“user”=>{“name”=>“q”, “city”=>“q”,
“zip”=>“q”, “password_confirmation”=>“qqqq”, “country_id”=>“1”,
“address_two”=>“q”, “phone”=>“q”, “first_name”=>“q”, “address_one”=>“q”,
“password”=>“qqqq”, “state”=>“q”, “email”=>“[email protected]”},
“commit”=>“Save”, “action”=>“create”, “controller”=>“users”,
“redirect”=>""}

in this case it was for a form ‘user’
so params[:user][:email] would give the result [email protected]

Thorsten M. wrote:

this depend on some details, mainly how the form is created and named

normaly you would get your params like params[:form_name][:field_name]
so if you have something like form_for :units

then
params[:units][:quantity_1]

should give you a result

otherwise look in you development.log. after submitting your form it
should look something like this:
(the Parameters part is of interest)

Processing UsersController#create (for 127.0.0.1 at 2007-11-26 12:08:32)
[POST]
Session ID: Parameters: {“user”=>{“name”=>“q”, “city”=>“q”,
“zip”=>“q”, “password_confirmation”=>“qqqq”, “country_id”=>“1”,
“address_two”=>“q”, “phone”=>“q”, “first_name”=>“q”, “address_one”=>“q”,
“password”=>“qqqq”, “state”=>“q”, “email”=>“[email protected]”},
“commit”=>“Save”, “action”=>“create”, “controller”=>“users”,
“redirect”=>""}

in this case it was for a form ‘user’
so params[:user][:email] would give the result [email protected]

hi,
The view is:
<% i = 0 %>
<% @units.each do |unit| %>
<% i = i + 1 %>

" name="productprice[quantity]" type="text" value="<%= unit.units%>">
<% end %>

Controller is:
@product=Product.find(params[“id”])
@units = Unit.find :all
@length = @units.length
@length.times do
@productprice = Productprice.new(params[:productprice])
if @productprice.save
end
end

Th parameters are:
“productprice”=>{“price”=>“2”, “quantity”=>“50”, “product_id”=>“3”}

Can you tell me where is the error?

name must be unique, too
in your view:

name=“productprice[quantity_#{unit.id}]”

for which then you would have to change your controller to
go through all fields

Thorsten M. wrote:

name must be unique, too
in your view:

name=“productprice[quantity_#{unit.id}]”

for which then you would have to change your controller to
go through all fields

Thanks a lot for replying.
I have changed my view.
<% i = 0 %>
<% @units.each do |unit| %>
<% i = i + 1 %>

" name="productprice[quantity<%= i %>]" type="text" value="<%= unit.units%>">
<% end %>

The controller is:
def add_price
@product=Product.find(params[“id”])
@units = Unit.find :all
@length = @units.length
@length.times do
@productprice = Productprice.new(params[:productprice])
if @productprice.save
end
end

Now the parameters are:
“productprice”=>{“product_id”=>“3”, “quantity1”=>“50”,
“quantity2”=>“1000”, “price1”=>“3”, “price2”=>“4”}}

But it is giving error:
undefined method `quantity1=’ for #Productprice:0x466f450

Because it might be looking in the database for field named quantity1
and its not getting.

Because it might be looking in the database for field named quantity1
that’s right, so far

you can’t use form_for in that case, but must fall back to the normal
form_tag instead, then manually going through the returned parameters
hash by generating the hash-keys rg
params[productprice][“price#{counter}”]

there are several ways to handle a problem like this, using one form
with multiple numbered fields or using multiple forms and keep the
fieldnames (and submit-buttons). it’s up to you, to make the right
decision.

Rohit M. wrote:

Thorsten M. wrote:

name must be unique, too
in your view:

name=“productprice[quantity_#{unit.id}]”

for which then you would have to change your controller to
go through all fields

Thanks a lot for replying.
I have changed my view.
<% i = 0 %>
<% @units.each do |unit| %>
<% i = i + 1 %>

" name="productprice[quantity<%= i %>]" type="text" value="<%= unit.units%>">
<% end %>

The controller is:
def add_price
@product=Product.find(params[“id”])
@units = Unit.find :all
@length = @units.length
@length.times do
@productprice = Productprice.new(params[:productprice])
if @productprice.save
end
end

Now the parameters are:
“productprice”=>{“product_id”=>“3”, “quantity1”=>“50”,
“quantity2”=>“1000”, “price1”=>“3”, “price2”=>“4”}}

But it is giving error:
undefined method `quantity1=’ for #Productprice:0x466f450

Because it might be looking in the database for field named quantity1
and its not getting.

hi,

Please provide me the solution of this problem.

Thanks,
Rohit.

Thanks a lot for help.

The code is working now.

Thorsten M. wrote:

Because it might be looking in the database for field named quantity1
that’s right, so far

you can’t use form_for in that case, but must fall back to the normal
form_tag instead, then manually going through the returned parameters
hash by generating the hash-keys rg
params[productprice][“price#{counter}”]

there are several ways to handle a problem like this, using one form
with multiple numbered fields or using multiple forms and keep the
fieldnames (and submit-buttons). it’s up to you, to make the right
decision.

Hi,

My controller is:
@product=Product.find(params[“id”])
@units = Unit.find :all
i = 0
@units.each do |unit|
i=i+1
@productprice = Productprice.new(params[:productprice])
@productprice.quanity = request.parameters[“quanity#{i}”]
@productprice.quanity = request.parameters[“unit#{i}”]
end
end

But its giving error:
undefined method `quanity=’ for #

Please tell me wher i am wrong?