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
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
i think you have to pass remove_product a Product object.
Try: @cart.remove_product(Product.find(params[:id]))
–jake
Shagy M. 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
Shagy M. 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
actually, nm. I realized that the problem was already solved.
sorry
–jake
Jake V. wrote:
i think you have to pass remove_product a Product object.
Try: @cart.remove_product(Product.find(params[:id]))
–jake
Shagy M. 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