RJS to find an element

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”.

page[:cart].replace_html :partial => ‘cart’, :object => @cart

page[@div].visual_effect :highlight,
:startcolor => “#88ff88”,
:endcolor => “#114411

Is there a way to use RJS to see if page[@div] (in this case) exists
before I try to do the highlight?

Thanks,
Jake

Jake S. wrote:

Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails

Check out
http://www.akropolix.net/rik0/blogs/2006/06/14/rjs-rails-if-statement/
It shows how to select the div if it exists and then run some javascript
happiness on it.

Matthew M.
blog.mattmargolis.net

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.

page[:cart].replace_html :partial => ‘cart’, :object => @cart

page.select(@div).each do |page|

page[page].visual_effect :highlight,
:startcolor => “#88ff88”,
:endcolor => “#114411
end

Thanks,
Jake

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.

_Kevin
www.sciwerks.com

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

Remobe_item.rjs:

page[:cart].replace_html :partial => ‘cart’, :object => @cart

if @div[1] == 1
page[@div[0]].visual_effect :highlight,
:startcolor => “#88ff88”,
:endcolor => “#114411
end

Thanks,
Jake

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs