Can anybody explain to me why params[:id] is evaluated as a string instead of an int? If I do this: # # Remove the selected product from the cart. # def remove_from_cart product = Product.find(params[:id]) @cart= find_cart @cart.remove_product(product.id) redirect_to(:action => ''display_cart'') end Everything works. If I do this: # # Remove the selected product from the cart. # def remove_from_cart @cart= find_cart @cart.remove_product(params[:id]) redirect_to(:action => ''display_cart'') end It doesn''t work. However, if I do THIS: # # Remove the selected product from the cart. # def remove_from_cart @cart= find_cart @cart.remove_product(params[:id].to_i) redirect_to(:action => ''display_cart'') end It works. Why is that? Sure, I can change the string (params[:id]) passed to remove_product to an int in the remove_product method, but should I have to? I can eliminate a database call by using to_i, but I don''t think I should have to specify to_i.... What do you think? Is there an easier way? Am I doing something wrong? Shagy -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Because everything that comes back through POST or GET is a string. That''s the way it is in Perl, PHP, Python, Ruby, etc. As for remove_product, I guess I''d have to see the source of that method, as I don''t recall, unless this is a new feature, methods like ''remove_'' being generated by belongs_to or has_many. Try @cart.delete(Product.find(params[:id]). More verbose, but that''s all I can think of right now. Jason On 12/14/06, Shagy Moe <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Can anybody explain to me why params[:id] is evaluated as a string > instead of an int? > > If I do this: > > # > # Remove the selected product from the cart. > # > def remove_from_cart > product = Product.find(params[:id]) > @cart= find_cart > @cart.remove_product(product.id) > redirect_to(:action => ''display_cart'') > end > > Everything works. > > If I do this: > > # > # Remove the selected product from the cart. > # > def remove_from_cart > @cart= find_cart > @cart.remove_product(params[:id]) > redirect_to(:action => ''display_cart'') > end > > It doesn''t work. However, if I do THIS: > > # > # Remove the selected product from the cart. > # > def remove_from_cart > @cart= find_cart > @cart.remove_product(params[:id].to_i) > redirect_to(:action => ''display_cart'') > end > > It works. Why is that? Sure, I can change the string (params[:id]) > passed to remove_product to an int in the remove_product method, but > should I have to? I can eliminate a database call by using to_i, but I > don''t think I should have to specify to_i.... > > What do you think? Is there an easier way? Am I doing something wrong? > > Shagy > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
i think you have to pass remove_product a Product object. Try: @cart.remove_product(Product.find(params[:id])) --jake Shagy Moe wrote:> Can anybody explain to me why params[:id] is evaluated as a string > instead of an int? > > If I do this: > > # > # Remove the selected product from the cart. > # > def remove_from_cart > product = Product.find(params[:id]) > @cart= find_cart > @cart.remove_product(product.id) > redirect_to(:action => ''display_cart'') > end > > Everything works. >> It doesn''t work. However, if I do THIS: > > # > # Remove the selected product from the cart. > # > def remove_from_cart > @cart= find_cart > @cart.remove_product(params[:id].to_i) > redirect_to(:action => ''display_cart'') > end > > It works. Why is that? Sure, I can change the string (params[:id]) > passed to remove_product to an int in the remove_product method, but > should I have to? I can eliminate a database call by using to_i, but I > don''t think I should have to specify to_i.... > > What do you think? Is there an easier way? Am I doing something wrong? > > Shagy-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
actually, nm. I realized that the problem was already solved. sorry --jake Jake Varghese wrote:> i think you have to pass remove_product a Product object. > > Try: @cart.remove_product(Product.find(params[:id])) > > > --jake > > Shagy Moe wrote:>> It doesn''t work. However, if I do THIS: >> >> # >> # Remove the selected product from the cart. >> # >> def remove_from_cart >> @cart= find_cart >> @cart.remove_product(params[:id].to_i) >> redirect_to(:action => ''display_cart'') >> end >> >> It works. Why is that? Sure, I can change the string (params[:id]) >> passed to remove_product to an int in the remove_product method, but >> should I have to? I can eliminate a database call by using to_i, but I >> don''t think I should have to specify to_i.... >> >> What do you think? Is there an easier way? Am I doing something wrong? >> >> Shagy-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Shagy Moe wrote the following on 14.12.2006 15:42 :> Can anybody explain to me why params[:id] is evaluated as a string > instead of an int? >"params" is a hash which AFAIK can only contain hashes, arrays or strings because it''s the result of the parsing of the HTTP request. As HTTP doesn''t even know that integers exist, all things parsed from HTTP are ultimately structures holding strings. Lionel --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---