What is wrong in this method?


#1

Hi,

This is my definition of method “add_to_cart”

def add_to_cart
count=params[:product][:count]
while count.to_i>=0.to_i
if params[:product][‘dem_quantity’+count.to_s]==""

  else
    @product = 

Product.find(params[:product][‘product_id’+count.to_s])
print @product.name
@product.update_attributes(params[:product][‘dem_quantity’+count.to_s])
end
count=count.to_i-1
end
end

With this method I am trying to just update values of the field “demand”
in the database corresponding to particular “id”. But when I m executing
this I m getting error like:-

undefined method `stringify_keys!’ for “5”:String

Application Trace of error is:-

C:/local/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1333:in
attributes=' C:/local/ruby/lib/ruby/gems/1.8/gems/activerecord-1.13.2/lib/active_record/base.rb:1266:inupdate_attributes’
#{RAILS_ROOT}/app/controllers/products_controller.rb:15:in `add_to_cart’
-e:3

What is wrong in above method?

Please tell me.
Thanx in advance.
Prash


#2

Am Mittwoch, den 22.03.2006, 10:15 +0100 schrieb Prashant T.:

    @product = 

in the database corresponding to particular “id”. But when I m executing
update_attributes' #{RAILS_ROOT}/app/controllers/products_controller.rb:15:inadd_to_cart’
-e:3

What is wrong in above method?

The error tells you, that you have a String where a Hash was expected.
Make sure params[:product] contains a Hash.


Norman T.

http://blog.inlet-media.de


#3

Prashant T. wrote:

	@product.update_attributes(params[:product]['dem_quantity'+count.to_s])
  end
  count=count.to_i-1
end

end

What is wrong in above method?

I see you assign count as a string from the params, then start a while
loop using that string.

Then you do “count.to_s” which attempts to stringify a string.

That, or the count.to_i after you assign it to a numeric object.

Chris