# Using data in application.rhtml layout

**URL:** <https://www.ruby-forum.com/t/using-data-in-application-rhtml-layout/79754>\
**Category:** Rails\
**Created:** [December 18, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/using-data-in-application-rhtml-layout/79754 "2006-12-18T18:58:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![daborg69](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d9b06d/32.png) [@daborg69](https://www.ruby-forum.com/u/daborg69)\
**Post date:** [December 18, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/using-data-in-application-rhtml-layout/79754/1 "2006-12-18T18:58:58Z")

</div>

I’m setting up a forum and I’m having a sidebar which will display the  
recent postings and will be used throughout the site. This sidebar is  
part of the application.rhtml layout.

How do I use data from the “forum” controller in this layout when the  
layout is used on all of the other controllers across the site?

Could someone point me in the right direction?

Many thanks - when I get a little more advanced I hope I can contribute  
as much advice as you guys have so kindly given me 🙂

---

<div class="post-metadata">

**Author:** ![daborg69](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d9b06d/32.png) [@daborg69](https://www.ruby-forum.com/u/daborg69)\
**Post date:** [December 18, 2006, 7:08pm UTC](https://www.ruby-forum.com/t/using-data-in-application-rhtml-layout/79754/2 "2006-12-18T19:08:01Z")

</div>

I’d say the nicest/cleanest thing to do would be to set that sidebar to  
be  
populated via AJAX. When the page loads, send of a request to the forum  
controller and update the sidebar div.

Jason

---

<div class="post-metadata">

**Author:** ![daborg69](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d9b06d/32.png) [@daborg69](https://www.ruby-forum.com/u/daborg69)\
**Post date:** [December 18, 2006, 7:16pm UTC](https://www.ruby-forum.com/t/using-data-in-application-rhtml-layout/79754/3 "2006-12-18T19:16:00Z")

</div>

Jason R. wrote:

> I’d say the nicest/cleanest thing to do would be to set that sidebar to  
> be  
> populated via AJAX. When the page loads, send of a request to the forum  
> controller and update the sidebar div.
> 
> Jason

Thanks but AJAX is something on my list to learn after I’ve got ror  
under my belt! 🙂

Is there any other way to do it with application\_help, or by rendering  
partials etc?

Thanks

---

<div class="post-metadata">

**Author:** ![daborg69](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d9b06d/32.png) [@daborg69](https://www.ruby-forum.com/u/daborg69)\
**Post date:** [December 18, 2006, 7:36pm UTC](https://www.ruby-forum.com/t/using-data-in-application-rhtml-layout/79754/4 "2006-12-18T19:36:05Z")

</div>

Well, here’s all the code you would need to get the AJAX working:

application.rhtml:  
…

 \<%= javascript\_tag remote\_function(:update =\> 'sidebar', :url =\> {:controller =\> 'forum\_controller', :action =\> 'sidebar\_action' }) %\>

I’m assuming the action on the forum controller already exists, and  
you’d  
just need a partial that renders just those items.

However, if you really want a non-ajax format, I guess you would need a  
method on the Forum model (if you don’t already have that) and then in  
your  
application.rb you can have:

class Application …  
after\_filter :fill\_in\_sidebar

def fill\_in\_sidebar  
@sidebar\_posts = Forum.find\_newest\_posts  
end  
end

This will call #fill\_in\_sidebar after any action on any controller.

Hope that helps.

Jason

---

<div class="post-metadata">

**Author:** ![daborg69](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d9b06d/32.png) [@daborg69](https://www.ruby-forum.com/u/daborg69)\
**Post date:** [December 18, 2006, 7:42pm UTC](https://www.ruby-forum.com/t/using-data-in-application-rhtml-layout/79754/5 "2006-12-18T19:42:07Z")

</div>

Scott H. wrote:

> as much advice as you guys have so kindly given me 🙂

Use a partial (perhaps in a /shared folder) and populate it with an  
instance variable set up in the application controller using a  
before\_filter.

So something like this

\_recent\_postings\_sidebar.rhtml

```
<% @recent_posts.each do |post| %>
    <%= post.title %>
<% end %>

```

in the application controller  
:before\_filter :setup\_sidebar

```
def setup_sidebar
    @recent_posts = Post.find(:all, :limit => 20, :order =>

```

“created\_at DESC”)  
end

and then in your layout:

render :partial =\> “/shared/recent\_postings\_sidebar”

---

<div class="post-metadata">

**Author:** ![daborg69](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d9b06d/32.png) [@daborg69](https://www.ruby-forum.com/u/daborg69)\
**Post date:** [December 24, 2006, 12:14am UTC](https://www.ruby-forum.com/t/using-data-in-application-rhtml-layout/79754/6 "2006-12-24T00:14:54Z")

</div>

> So something like this
> 
> \_recent\_postings\_sidebar.rhtml
> 
> ```
> <% @recent_posts.each do |post| %>
> <%= post.title %>
> <% end %>
> 
> ```
> 
> in the application controller  
> :before\_filter :setup\_sidebar
> 
> ```
> def setup_sidebar
> @recent_posts = Post.find(:all, :limit => 20, :order =>
> 
> ```
> 
> “created\_at DESC”)  
> end
> 
> and then in your layout:
> 
> render :partial =\> “/shared/recent\_postings\_sidebar”

This works great on my local machine, (using webrick) but when I use it  
on an apache fcgi server (dreamhost) the variable does not update after  
it has been initially set.

Anyone have any ideas how to fix this?

---

<div class="post-metadata">

**Author:** ![daborg69](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/d9b06d/32.png) [@daborg69](https://www.ruby-forum.com/u/daborg69)\
**Post date:** [December 24, 2006, 12:44am UTC](https://www.ruby-forum.com/t/using-data-in-application-rhtml-layout/79754/7 "2006-12-24T00:44:17Z")

</div>

On Dec 18, 6:37 pm, Chris T [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > Many thanks - when I get a little more advanced I hope I can contribute  
> > \<% end %\>
> 
> render :partial =\> “/shared/recent\_postings\_sidebar”

That worked great on my local machine, but when I uploaded it to a  
server using fcgi the variable does not update once it has been  
initially set.

Any ideas?
