2

I iterate over an array of CartItem class I created and populate a view file.

<td><%= button_to "remove", :action => :remove_from_cart, :id => cart_item %></td>

I would like to be able to get a CartItem instance from params[:id] in the remove_from_cart. param[:id] returns something like "#<CartItem:0xb77a3dcc>":String and i could not figure out how to treat it like a CartItem object.

I know the code above works for objects descending from ActiveRecord::Base, and CartItem does not descend from it. I am guessing that might be the reason.

any pointers would be much appreciated, thanks

1 Answer 1

1

Change the code to pass a CartItem id instead of a CartItem object. If we assume CartItem class has a method called id then following code should work.

<%= button_to "remove", :action => :remove_from_cart, :id => cart_item.id %>
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer. So, should I assume that it is not possible to pass objects as parameters unless they descend from ActiveRecord::Base ?
Yes. Code treats the ActiveRecord::Base classes as special classes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.