# Application.html.erb problem

**URL:** <https://www.ruby-forum.com/t/application-html-erb-problem/165973>\
**Category:** Rails\
**Created:** [April 20, 2009, 2:33pm UTC](https://www.ruby-forum.com/t/application-html-erb-problem/165973 "2009-04-20T14:33:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![preethi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@preethi](https://www.ruby-forum.com/u/preethi)\
**Post date:** [April 20, 2009, 2:33pm UTC](https://www.ruby-forum.com/t/application-html-erb-problem/165973/1 "2009-04-20T14:33:09Z")

</div>

Hi,  
This may be a silly question to ask. But as performance issues start  
popping up I had no other go other than searching for a solution.  
I’ve application.html.erb where i’ve defined the layout of my whole  
project, where i’ve so many db calls and everything which is a one time  
call that must happen for the whole layout.  
In the main area i’ve

```
               <%= yield %>

```

I want only this part to be refreshed/changed whenever i go back and  
forth of the pages, but the problem is, the whole page is getting  
refreshed everytime which is consuming hell a lot of time as i’m doing  
so many db calls for the common layout.

How to achieve my requirement and improve the performance?  
And if someother tips are there the improve the performance in ruby on  
rails  
Thanks in advance 🙂

---

<div class="post-metadata">

**Author:** ![preethi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@preethi](https://www.ruby-forum.com/u/preethi)\
**Post date:** [April 20, 2009, 3:05pm UTC](https://www.ruby-forum.com/t/application-html-erb-problem/165973/2 "2009-04-20T15:05:18Z")

</div>

Gavin M. wrote:

> You shouldn’t be making database calls from the view.  
> If you’re making calls for the application layout then I’d add these  
> to the application controller and call them with a before\_filter  
> (anyone know a better way ?)
> 
> If the objects you are calling are the same from one page to the next  
> you could try using an “or equals”
> 
> so instead of always calling  
> @current\_user = User.find session[:user\_id]

Instance variables (@current\_user) don’t survive from request to  
request, so a lookup will be done for every request (in other words,  
from one page to the next). You could save these objects in your session  
and if you store sessions in memcached, things will speed up and no  
queries to the db will be needed.  
To speed up things, caching is a good thing. There are loads of  
tutorials about this and it caveats.

---

<div class="post-metadata">

**Author:** ![preethi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@preethi](https://www.ruby-forum.com/u/preethi)\
**Post date:** [April 20, 2009, 2:43pm UTC](https://www.ruby-forum.com/t/application-html-erb-problem/165973/3 "2009-04-20T14:43:32Z")

</div>

You shouldn’t be making database calls from the view.  
If you’re making calls for the application layout then I’d add these  
to the application controller and call them with a before\_filter  
(anyone know a better way ?)

If the objects you are calling are the same from one page to the next  
you could try using an “or equals”

so instead of always calling  
@current\_user = User.find session[:user\_id]

you could write @current\_user ||= User.find session[:user\_id]

This should mean that the database call is only made if @current\_user  
is nil

does that help?

On Apr 20, 1:33 pm, Preethi S. \<rails-mailing-l…@andreas-

---

<div class="post-metadata">

**Author:** ![preethi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@preethi](https://www.ruby-forum.com/u/preethi)\
**Post date:** [April 20, 2009, 3:14pm UTC](https://www.ruby-forum.com/t/application-html-erb-problem/165973/4 "2009-04-20T15:14:03Z")

</div>

you’re right Wouter- that was dumb of me

On Apr 20, 2:05 pm, Wouter de Bie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![preethi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@preethi](https://www.ruby-forum.com/u/preethi)\
**Post date:** [April 20, 2009, 3:37pm UTC](https://www.ruby-forum.com/t/application-html-erb-problem/165973/5 "2009-04-20T15:37:49Z")

</div>

Gavin M. wrote:

> You shouldn’t be making database calls from the view.  
> If you’re making calls for the application layout then I’d add these  
> to the application controller and call them with a before\_filter  
> (anyone know a better way ?)
> 
> If the objects you are calling are the same from one page to the next  
> you could try using an “or equals”
> 
> so instead of always calling  
> @current\_user = User.find session[:user\_id]
> 
> you could write @current\_user ||= User.find session[:user\_id]
> 
> This should mean that the database call is only made if @current\_user  
> is nil
> 
> does that help?
> 
> On Apr 20, 1:33?pm, Preethi S. \<rails-mailing-l…@andreas-

Thanks for your response.  
But, Am not making any db calls from view. I’ve helpers for everything  
which renders some tabs for user depending on his view. The queries for  
fetching the permissions that the person has and everything is fectched  
from db everytime the page is loaded even though that part of the layout  
is static once it is fectched for the first time. It gets refreshed  
everytime. That is the problem. i want only part of my page to be  
refreshed and other parts to remain static once loaded. How to achieve  
that?

---

<div class="post-metadata">

**Author:** ![preethi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/f04885/32.png) [@preethi](https://www.ruby-forum.com/u/preethi)\
**Post date:** [April 20, 2009, 3:56pm UTC](https://www.ruby-forum.com/t/application-html-erb-problem/165973/6 "2009-04-20T15:56:22Z")

</div>

On Apr 20, 2:37 pm, Preethi S. \<rails-mailing-l…@andreas-  
[s.net](http://s.net)\> wrote:

> Thanks for your response.  
> But, Am not making any db calls from view. I’ve helpers for everything  
> which renders some tabs for user depending on his view. The queries for  
> fetching the permissions that the person has and everything is fectched  
> from db everytime the page is loaded even though that part of the layout  
> is static once it is fectched for the first time. It gets refreshed  
> everytime. That is the problem. i want only part of my page to be  
> refreshed and other parts to remain static once loaded. How to achieve  
> that?

You could either avoid traditional navigation and use ajax to update  
bits of the page as required or use fragment cacheing to cache the  
bits of html that are expensive to render or cache the data that sits  
behind that.

Fred
