<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
  <channel>
    <title>Recent Posts in 'Highlight effect for ajax cart' | Pragmatic Forums</title>
    <link>http://forums.pragprog.com/forums/66/topics/405</link>
    <language>en-us</language>
    <ttl>60</ttl>
    <description></description>
    <item>
      <title>Highlight effect for ajax cart posted by Michael Curtis @ Sun, 04 May 2008 17:58:38 -0000</title>
      <description>&lt;p&gt;That did it, thanks for the reply!&lt;/p&gt;</description>
      <pubDate>Sun, 04 May 2008 17:58:38 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:66:405:2729</guid>
      <author>Michael Curtis</author>
      <link>http://forums.pragprog.com/forums/66/topics/405</link>
    </item>
    <item>
      <title>Highlight effect for ajax cart posted by Sam Ruby @ Sun, 04 May 2008 00:18:17 -0000</title>
      <description>&lt;p&gt;The else clause in &lt;a href="http://media.pragprog.com/titles/rails3/code/depot_m/app/models/cart.rb"&gt;cart.rb&lt;/a&gt; should read:&lt;/p&gt;


&lt;pre&gt;
current_item = CartItem.new(product)
@items &amp;lt;&amp;lt; current_item
&lt;/pre&gt;</description>
      <pubDate>Sun, 04 May 2008 00:18:17 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:66:405:2721</guid>
      <author>Sam Ruby</author>
      <link>http://forums.pragprog.com/forums/66/topics/405</link>
    </item>
    <item>
      <title>Highlight effect for ajax cart posted by Michael Curtis @ Fri, 02 May 2008 19:55:56 -0000</title>
      <description>&lt;p&gt;For some reason, the id=&amp;#8221;current_item&amp;#8221; only gets set the second time I add an item to a cart. The first time it just gets placed in a naked &amp;lt;tr&gt; tag.&lt;/p&gt;


	&lt;p&gt;Anybody see something wrong with my code?&lt;/p&gt;


	&lt;p&gt;cart.rb&lt;br /&gt;&lt;pre&gt;
&lt;code&gt;
  def add_product(product)
    current_item = @items.find {|item| item.product == product}
    if current_item
      current_item.increment_quantity
    else
      @items &amp;lt;&amp;lt; CartItem.new(product)
    end
    current_item
  end
&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;store_controller.rb&lt;br /&gt;&lt;pre&gt;
&lt;code&gt;
  def add_to_cart
    begin
      product = Product.find(params[:id])
    rescue ActiveRecord::RecordNotFound
      logger.error("Attempt to access invalid product #{params[:id]}")
      redirect_to_index("Invalid product")
    else
      @cart = find_cart
      @current_item = @cart.add_product(product)
      respond_to { |format| format.js }
    end
  end
&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;_cart_item.html.erb&lt;br /&gt;&lt;pre&gt;
&lt;code&gt;
&amp;lt;% if cart_item == @current_item %&amp;gt;
  &amp;lt;tr id="current_item"&amp;gt;
&amp;lt;% else %&amp;gt;
  &amp;lt;tr&amp;gt;
&amp;lt;% end %&amp;gt;
  &amp;lt;td&amp;gt;&amp;lt;%= cart_item.quantity %&amp;gt;&amp;amp;times;&amp;lt;/td&amp;gt;
  &amp;lt;td&amp;gt;&amp;lt;%= h(cart_item.title )%&amp;gt;&amp;lt;/td&amp;gt;
  &amp;lt;td class="item-price"&amp;gt;&amp;lt;%= number_to_currency(cart_item.price) %&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;/tr&amp;gt;
&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;


	&lt;p&gt;add_to_cart.js.rjs&lt;br /&gt;&lt;pre&gt;
&lt;code&gt;
page.replace_html("cart", :partial =&amp;gt; "cart", :object =&amp;gt; @cart)

page[:current_item].visual_effect :highlight,
                                  :start_color =&amp;gt; "#88ff88",
                                  :end_color =&amp;gt; "#114411" 
&lt;/code&gt;&lt;br /&gt;&lt;/pre&gt;&lt;/p&gt;</description>
      <pubDate>Fri, 02 May 2008 19:55:56 -0000</pubDate>
      <guid isPermaLink="false">forums.pragprog.com:66:405:2710</guid>
      <author>Michael Curtis</author>
      <link>http://forums.pragprog.com/forums/66/topics/405</link>
    </item>
  </channel>
</rss>
