artsince
artsince

Reputation: 1032

rails retrieving button_to parameter in the controller

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

Upvotes: 2

Views: 1920

Answers (1)

Harish Shetty
Harish Shetty

Reputation: 64363

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 %>

Upvotes: 1

Related Questions