Forum: Ruby on Rails What is wrong in this method??????

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.
8c70506408fddb9740b4de7339055c77?d=identicon&s=25 Prashant Tiwari (tiwaripk)
on 2006-03-22 10:15
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:in
`update_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
Bf66e10c8fc4abefebde0425e7f6f15a?d=identicon&s=25 Norman Timmler (Guest)
on 2006-03-22 11:17
(Received via mailing list)
Am Mittwoch, den 22.03.2006, 10:15 +0100 schrieb Prashant Tiwari:
>         @product =
> in the database corresponding to particular "id". But when I m executing
> `update_attributes'
> #{RAILS_ROOT}/app/controllers/products_controller.rb:15:in `add_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 Timmler

http://blog.inlet-media.de
75ceef25624b3e44943a3b7319ba9612?d=identicon&s=25 Chris Schumann (torgo)
on 2006-03-22 16:45
(Received via mailing list)
Prashant Tiwari 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
This topic is locked and can not be replied to.