Div is shown only from the layout nowhere else

Hi guys!
I have a little problem with my code. I have a div in my store layout,
inside of which i have my cart. When i put something in the cart i get
an AJAX blind_down and a replace_html. I can see the cart when i am in
the store controller (http://localhost/store), but when i go to
another controller (e.g localhost/products/show_each/5) the cart is
gone.

This is the div in the “store.html.erb” file in my layout folder:

<% if @cart %> <% hidden_div_if(@cart.items.empty?, :id => "cart" ) do %> <%= render(:partial => "cart" , :object => @cart) %> <% end %> <% end %>
   <div id="side_right_login">
     Login form
   </div>
</div>

Inside my “index.html.erb” in my store folder i use this method for
adding a product:
<% form_remote_tag :url => {:action => :add_to_cart, :id => product }
do %>
<%= submit_tag “Add to cart” %>
<% end %>

Also this is the AJAX code “add_to_cart.js” which is inside the store
folder (if this helps):

page.select(“div#notice”).each { |div| div.hide}

page.replace_html(“cart”, :partial=>“cart”, :object=>@cart)

page[:cart].visual_effect :blind_down if @cart.total_items==1

page[:current_item].visual_effect :highlight,
:start_color => “#88ff88”,
:endcolor => “#114411”,
:duration => 3.0

Any comment would be helpful!
Thanx in advance
Kostas

Are you creating the @cart instance variable? Your code suggests that
this is null:

<% if @cart %>

Try some debug code in your layout to see what @cart is,

<%= debug @cart %>

or better still right a test.

Cheers,
Nicholas

Nichola,
unfortunately i am not able to write tests at this time.

When i use debug in the store i get the products that i have in my
cart. But when i use debug in some other controller e.g products, i
get nothing.

Kostas

Yes you are correct. I am only creating a @cart variable in the store
controller.

Kostas L. wrote:

Nichola,
unfortunately i am not able to write tests at this time.
[…]

Huh? Why not? What’s the problem?

In any case, you should probably fix that problem before going further
– there is no substitute for well-tested code.

Best,

Marnen Laibow-Koser
http://www.marnen.org
[email protected]

So I guess that means in the other controllers you are not creating a
variable @cart. Does that make sense?

OK, so you know what you need to do then?

An instance variable @x survives only for one HTTP request. When you
move from the store controller to some other controller, most likely,
you’re sending another HTTP request (as opposed to an AJAX request,
typically) which clears out all instance variables. This clears out
@cart and you dont see your data.

For each request that you want your @cart to survive in, you need to
populate it with values in the controller. How can you do this? use
before_filters maybe. depends on your setup really.

No i dont. I am confused. Any suggestions?? Thinking of using a partial
but
i am not quite sure.

On Mon, Jun 22, 2009 at 6:55 PM, Nicholas H.
[email protected]wrote:

variable @cart. Does that make sense?

Kostas

I have a little problem with my code. I have a div in my store
This is the div in the “store.html.erb” file in my layout folder:

do %>


Κωνσταντίνος Λούπος
[email protected]