I’m trying to implement a remove from cart method based on the
examples in the second edition Agile book. I have everything working
except for the RJS that removes items from the cart. Ideally what
I’d like to do is highlight an element that I am deleting only if
there is more than one of that particular item in the cart. If the
item deleted is the only of it’s kind it gets removed from the
document before the highlight is “activated”.
Matthew,
Thanks for your reply, however, I can’t seem to get it to work
properly. Maybe it is because in certain cases I am removing that
element from the document before I get to the “search”, or maybe
there is an error in my implementation.
On Monday, July 31, 2006, at 11:13 AM, Jake S. wrote:
On Jul 31, 2006, at 10:51 AM, Matthew M. wrote:
Jake
http://lists.rubyonrails.org/mailman/listinfo/rails
I’m beginning to think that RJS templates should provide NO helper
functions at all. Just a big text block that gets sent back. That way
we would all have to program the response in JS, and we wouldn’t get
tempted to use ruby control structures. It can be very confusing for
people to grasp the concept that the RJS output is executed in the
context of the client browser, rather than on the server side.
Here’s what I did to solve my problem, it seems dirty and clunky. If
anybody has a better solution, I’m open to suggestions. From within
the model I’m returning an array (to the controller) that has the id
of the element to update as well as a variable that determines if the
highlight effect will be displayed by the RJS template.
Cart model:
def remove_cart_item(product)
current_item = @items.find { |item| item.product == product }
if current_item.quantity > 1
current_item.decrement_quantity #defined in cart_item model @effect = 1
else @items.delete(current_item) @effect = 0
end
current_item = [“product” + current_item.id.to_s, @effect]
current_item
end
Store controller:
def remove_item
begin @product = Product.find(params[:id])
rescue
logger.error(“Attempt to access invalid product #{params[:id]}”)
redirect_to_index(“Invalid product ‘#{params[:id]}’”)
else @div = @cart.remove_cart_item(@product)
redirect_to_index unless request.xhr?
end
end