# Div is shown only from the layout nowhere else

**URL:** <https://www.ruby-forum.com/t/div-is-shown-only-from-the-layout-nowhere-else/170768>\
**Category:** Rails\
**Created:** [June 22, 2009, 4:38pm UTC](https://www.ruby-forum.com/t/div-is-shown-only-from-the-layout-nowhere-else/170768 "2009-06-22T16:38:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![little\_kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@little\_kostas](https://www.ruby-forum.com/u/little_kostas)\
**Post date:** [June 22, 2009, 4:38pm UTC](https://www.ruby-forum.com/t/div-is-shown-only-from-the-layout-nowhere-else/170768/1 "2009-06-22T16:38:32Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![little\_kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@little\_kostas](https://www.ruby-forum.com/u/little_kostas)\
**Post date:** [June 22, 2009, 4:56pm UTC](https://www.ruby-forum.com/t/div-is-shown-only-from-the-layout-nowhere-else/170768/2 "2009-06-22T16:56:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![little\_kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@little\_kostas](https://www.ruby-forum.com/u/little_kostas)\
**Post date:** [June 22, 2009, 5:23pm UTC](https://www.ruby-forum.com/t/div-is-shown-only-from-the-layout-nowhere-else/170768/3 "2009-06-22T17:23:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![little\_kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@little\_kostas](https://www.ruby-forum.com/u/little_kostas)\
**Post date:** [June 22, 2009, 5:41pm UTC](https://www.ruby-forum.com/t/div-is-shown-only-from-the-layout-nowhere-else/170768/4 "2009-06-22T17:41:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![little\_kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@little\_kostas](https://www.ruby-forum.com/u/little_kostas)\
**Post date:** [June 22, 2009, 5:31pm UTC](https://www.ruby-forum.com/t/div-is-shown-only-from-the-layout-nowhere-else/170768/5 "2009-06-22T17:31:14Z")

</div>

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](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![little\_kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@little\_kostas](https://www.ruby-forum.com/u/little_kostas)\
**Post date:** [June 22, 2009, 5:34pm UTC](https://www.ruby-forum.com/t/div-is-shown-only-from-the-layout-nowhere-else/170768/6 "2009-06-22T17:34:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![little\_kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@little\_kostas](https://www.ruby-forum.com/u/little_kostas)\
**Post date:** [June 22, 2009, 5:57pm UTC](https://www.ruby-forum.com/t/div-is-shown-only-from-the-layout-nowhere-else/170768/7 "2009-06-22T17:57:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![little\_kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@little\_kostas](https://www.ruby-forum.com/u/little_kostas)\
**Post date:** [August 10, 2009, 11:20am UTC](https://www.ruby-forum.com/t/div-is-shown-only-from-the-layout-nowhere-else/170768/8 "2009-08-10T11:20:43Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![little\_kostas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/f9ae1b/32.png) [@little\_kostas](https://www.ruby-forum.com/u/little_kostas)\
**Post date:** [August 9, 2009, 5:20pm UTC](https://www.ruby-forum.com/t/div-is-shown-only-from-the-layout-nowhere-else/170768/9 "2009-08-09T17:20:13Z")

</div>

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.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)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 %\>
> 
> >

–  
ÎšÏ‰Î½ÏƒÏ„Î±Î½Ï„Î¯Î½Î¿Ï‚ Î›Î¿ÏÏ€Î¿Ï‚  
removed\_email\_address@domain.invalid
